python终端操作models

265 阅读1分钟

精选博客

1.进入到工程目录

from crm import models #crm代表app名称

2.通过表名获取app名字

dir(models.CustomerInfo._meta)

models.CustomerInfo._meta.app_label #'crm'

3.获取表名

models.CustomerInfo._meta.model

models.CustomerInfo._meta.model_name

4.利用反射

models.CustomerInfo._meta.fields #获取model每一个字段(对象)

models.CustomerInfo._meta.get_field('statis') #取一个字段的对象

5.获取choices

a = models.CustomerInfo._meta.get_field('statis')

a.choices

6.反射

a = models.CustomerInfo.objects.last()

getattr(a,'get_statis_display')()
#已退学