首页 > Web开发 > 详细

_Meta 部分用法

时间:2018-03-20 20:30:56      阅读:222      评论:0      收藏:0      [点我收藏+]

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 部分用法

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!