python 列出表中存在的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2368948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List fields present in a table
提问by Hulk
Is there any way to to list out the fields present in a table in django models
有没有办法列出django模型中表中存在的字段
class Profile(models.Model):
user = models.ForeignKey(User, unique=True)
name = models.ForeignKey(School)
emp = models.ForeignKey(User, unique=True)
How to list out the filed names from the table Profile,(just like desc Profile; in mysql )
如何从表 Profile 中列出文件名,(就像 desc Profile; in mysql )
thanks.
谢谢。
回答by Ignacio Vazquez-Abrams
Profile._meta.fields
will get you a list of fields. The name
property of the field object contains the name of the field. Profile._meta.get_fields_with_model()
will return a list of 2-tuples of (field, model)
, with model
being None
if the field is in Profile
.
Profile._meta.fields
会给你一个字段列表。name
字段对象的属性包含字段的名称。Profile._meta.get_fields_with_model()
将返回的2元组的列表(field, model)
,以model
作为None
如果该字段中Profile
。