列出表中的字段
有没有办法列出Django模型中表格里有哪些字段呢?
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
name = models.ForeignKey(School)
emp = models.ForeignKey(User, unique=True)
怎么能列出Profile表里的字段名,就像在MySQL里用desc Profile;那样?
谢谢。
1 个回答
19
Profile._meta.fields
可以让你得到一个字段的列表。每个字段对象的 name
属性里包含了这个字段的名字。而 Profile._meta.get_fields_with_model()
会返回一个包含两个元素的元组列表,格式是 (field, model)
,其中如果字段是在 Profile
里,model
就会是 None
。