| 关系数据库 Django可以支持关系数据库中的表结构的多对一、多对多、一对一多种方式。 多对一 使用ForeignKey class City(meta.Model): #.... class Place(meta.Model): #.... city = meta.ForeignKey(City) 通常情况下,ForeignKey中应该是对应的Model的名字,实际上系统会使用对应Model的_id来维持这个关系。 使用 meta.ForeignKey("self") 尽兴递归性的多对一 ForeignKey 参数: edit_inline 如果为否,不可在admin模式下编辑。 limit_choices_to 使用字典来限制admin模式下的选项。 limit_choice_to={'pub_date__lte' : meta.LazyDate()} max_num_in_admin min_num_in_admin num_extra_on_change num_in_admin raw_id_admin 都是用于admin模式 related_name 设置别名 对于一个对象相对多次关联另一个对象很有用 meta.ForeignKey( Category, related_name="primary_story") meta.ForeignKey(Category, related_name="secondary_story") # get_primary_story_list() get_secondary_story_list() 多对多 ManyToManyField 选项: related_name 同上 filter_interface admin模式下使用js而不是select limit_choices_to 同上 一对一 OneToOneField META 选项 非数据库字段 class Foo(meta.Model): bar = meta.CharField(maxlength=30) #.... class META: admin = meta.Admin() # .... 选项: admin 一个meta.Admin对象。表示是否在admin模式下可见 db_table db_table="pizza_orders" 否则采用app_lable+'_'+module_name给表命名 exceptions exceptions = ("aaa","bbb") ? get_latest_by get_lastedt_by = "order_date"对应 一个DateField或者DateTimeField。对象将可通过get_lastest()访问 module_constants 一个可用于module_level使用的常量定义字典 module_constants={'MEAT_TYPE_PEPPERONI':1,'META_TYPE_SAUSAGE':2,} module_name 如果不指定。系统自动使用 小写class名+'s' order_with_respect_to order_with_respet_to = 'pizza' 根据相关对象排序。如果有关联的话 ordering ordering = ['-order_date'] 真正的排序。get_list -号倒序 ?号乱序 permissions 使用二元数组或元组定义admin模式下permissions权限 ((permission_code, human_readable_permission_name),) unique_together 使用list of lists 定义。用于数据库创建或admin模式 unique_together = (("driver","restaurant"),) verbose_name human-readable version verbose_name_plural 复数版本 Admin 选项 date_hierarchy date_hierarchy='order_date' admin通过date过滤对象 fields 设置用来显示的字短列表。 [(name, field_options)] options是一个控制字段的字典。字典键值如下: fields 显示那些字段 classes css设置 fields = ( (None, { 'fields': ('url', 'title', 'content', 'sites') }), ('Advanced options', { 'classes': 'collapse', 'fields' : ('enable_comments', 'registration_required', 'template_name') }), ), 显示效果 500)this.width=500'> js 在admin模式下现实的一个js字符串列表 list_display 采用列表显示。 list_filter 打开过滤器。布朗型或多对一 list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff'),list_filter = ('is_staff', 'is_superuser'), 500)this.width=500'> ordering 排序 save_as 另存一个新对象 save_on_top 上方显示save search_fields 上图上面的searech 框 |