码迷,mamicode.com
首页 > Web开发 > 详细

_Meta 部分用法

时间:2018-03-20 20:41:14      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:pre   def   pass   ini   for   tom   信息   return   logs   

model

model._meta.model_name
model._meta.app_label
model._meta.get_field("字段")
   --------
   Book:
   list_filter=["state","publish","authors"]
   
   每一个字段相关信息:
    
        字段字符串  : "state"
        字段对象    : Book._meta.get_field("state")
        字段关联数据: 
               if---choice类型字段:
                     字段对象.choices
                     
               if---ForeignKey,ManytoMany:
                     字段对象.rel.to.objects.all()
        

   字段信息封装成类:
   
      class FilterField(object):
            def __init__(self,filter_field_name,filter_field_obj):
                self.filter_field_name=filter_field_name
                self.filter_field_obj=filter_field_obj

                
            def get_data(self):
                if isinstance(self.filter_field_obj,ForeignKey) or isinstance(self.filter_field_obj,ManyToManyField):
                    return self.filter_field_obj.rel.to.objects.all()
                elif self.filter_field_obj.choices:
                    return self.filter_field_obj.choices
                else:
                    pass
        
        
        state=FilterField("state",state_obj)

_Meta 部分用法

标签:pre   def   pass   ini   for   tom   信息   return   logs   

原文地址:https://www.cnblogs.com/iyouyue/p/8611925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!