_meta
official documentation for Model API https://docs.djangoproject.com/en/1.10/ref/models/meta/
Field Access API
>>> from django.contrib.auth.models import user# A field on the model>>> User._meta.get_field (' Username ') <django.db.models.fields.charfield:username># a field from another model which has A relation with the cur Rent model>>> User._meta.get_field (' LogEntry ') <manytoonerel:admin.logentry># A non existent field> >> User._meta.get_field (' does_not_exist ') Traceback (most recent call last): ... Fielddoesnotexist:user has no field named ' Does_not_exist '
The choices of this field access API can get a list of the contents of a table with a choice field (in the form of tuples), for a foreign key field through Get_choices the past contents of a tuple
For example, there is a customer table in model
Class Customer (models. Model): ' Customer table ' ' name = models. Charfield (max_length=32,verbose_name= ' Customer name ', blank=true,null=true) QQ = models. Charfield (max_length=64,verbose_name= ' QQ ', unique=true) Qq_name = models. Charfield (max_length=64,verbose_name= ' QQ name ', blank=true,null=true) phone = models. Charfield (max_length=64,blank=true,null=true,verbose_name= ' mobile number ') Source_choice = ((0, ' turn to introduce '), (1, ' QQ Group '), (2, ' official website '), (3, ' Baidu Promotion '), (4, ' 51CTO '), (5, ' Know '), (6, ' marketing '),) Source = models. Smallintegerfield (verbose_name= ' select Source ', choices=source_choice) Referral_from = models. Charfield (verbose_name= ' introducer ', max_length=64,blank=true,null=true) Consult_course = models. ForeignKey (' Course ', verbose_name= ' Consulting course ') Consultant = models. ForeignKey (' userprofile ', verbose_name= ' consultant ') content = models. TextField (verbose_name= ' Enquiry details ') status_choices = ((0, ' registered '), (1, ' not enrolled '), status = models. Smallintegerfield (choices=status_choices, default=1) date = models. Datetimefield (auto_now_add=true) memo = models. TextField (verbose_name= ' remarks ', blank=true,null=true) tags = models. Manytomanyfield (' Tag ') #,blank=true,null=true def __unicode__ (self): return SELF.QQ
Get all the content of choice by _meta.get_field (' Field name ')
The result of obj = Customer._meta.get_field (' source ') print obj.choices# output is ((0, ' go to introduce '), (1, ' QQ Group '), (2, ' official website '), (3, ' Baidu Promotion '), (4, ' 51CTO '), (5, ' Know '), (6, ' marketing '),) #那么得到的这个元组就可以用for循环来获取每个小元组项, and then front-end display
Foreign key tags to get the content, through Get_choices ()
obj = Customer._meta.get_field (' tags ') print obj.get_choices () #输出的结果为: [(' ', '---------'), (1, ' goser1158 '), (2, ' Haha ')] #同样也可以对列表进行for循环获取里面的小元组来展现到前端页面
Of course, you can also get the name of the foreign key in the foreign key field
obj = Customer._meta.get_field (' consultant ') print type (obj). The result of the __name__# output is: ' ForeignKey ' obj = customer._ Meta.get_field (' tags ') print type (obj). The result of the __name__# output is: ' Manytomanyfield ' #通过判断外键的名称就可以做进一步的处理操作
Django Model _meta API