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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:28:48  来源:igfitidea点击:

List fields present in a table

pythondjango

提问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.fieldswill get you a list of fields. The nameproperty 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 modelbeing Noneif the field is in Profile.

Profile._meta.fields会给你一个字段列表。name字段对象的属性包含字段的名称。Profile._meta.get_fields_with_model()将返回的2元组的列表(field, model),以model作为None如果该字段中Profile