django json字段充当模型实例,并支持django admin中的无缝内联编辑。
django-submodel的Python项目详细描述
一个django字段,其工作方式类似于模型实例,将子模型字段存储在单个db列中,并支持在django管理中进行平滑编辑。
django子模型适用于django 1.3+
安装
- pip install django-submodel
用法
# in you_app/models.py from django.db import models from submodel.fields import SubModelField # Define (field_name, model_field_instance) tuples in "fields" parameter in SubModelField, # similar as normal Model definition # If SubModelField such as pref field is newly added to an existed model, # you may want to use south to migrate the schema class UserProfile(models.Model): realname = models.CharField(max_length=100) pref = SubModelField(u'user preference', fields=( ('title', models.CharField(max_length=10)), ('color', models.IntegerField(choices=((0, 'Black'), (1, 'White')))), ('birthday_day', models.DateTimeField()), ('feel_luck', models.BooleanField(default=True))))
# in you_app/admin.py, to enable editing of sub fields of pref in Django admin from submodel.admin import SubModelFieldInlineAdmin from . models import UserProfile class PrefInilneAdmin(SubModelFieldInlineAdmin): model = UserProfile._meta.get_field('pref').submodel # TODO improve class UserProfileAdmin(admin.ModelAdmin): inlines = [PrefInilneAdmin] admin.site.register(UserProfile, UserProfileAdmin)
>>> # TODO in Python shell, the value of pref field as a model instance
待办事项
- add tests
- simpler API
- dump and modify Deserializer to support missing subfields. schema policy?
- better history
- check compatibility w/ django-reversion and others …
- possible config
- more strict check such as attname confliction, for example FK field user and field user_id