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)