如何获取 Django 模型的所有字段?
给定一个 Django 模型,我试图列出它的所有字段。我见过一些使用 _meta 模型属性执行此操作的示例,但是 meta 前面的下划线是否表明 _meta 属性是私有属性,不应直接访问? ...因为,例如,_meta 的布局将来可能会发生变化,并且不是一个稳定的 API?
_meta 是这条规则的例外吗?它是否稳定且可以使用,或者访问它是否被认为是不好的做法?或者是否有函数或其他方法可以在不使用 _meta 属性的情况下内省模型的字段?下面是一些链接的列表,显示了如何使用 _meta 属性来执行此操作
。非常感谢任何建议。
Given a Django model, I'm trying to list all of its fields. I've seen some examples of doing this using the _meta model attribute, but doesn't the underscore in front of meta indicate that the _meta attribute is a private attribute and shouldn't be accessed directly? ... Because, for example, the layout of _meta could change in the future and not be a stable API?
Is _meta an exception to this rule? Is it stable and ready to use or is it considered bad practice to access it? Or is there a function or some other way to introspect the fields of a model without using the _meta attribute? Below is a list of some links showing how to do this using the _meta attribute
Any advice is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
_meta
是私有的,但相对稳定。人们正在努力对其进行形式化、记录并删除下划线,这可能会发生在 1.3 或 1.4 之前。我想我们会努力确保向后兼容,因为无论如何都有很多人在使用它。如果您特别关心兼容性,请编写一个接受模型并返回字段的函数。这意味着如果将来某些事情发生变化,您只需更改一项功能即可。
我相信这将返回
Field
对象的列表。要从实例获取每个字段的值,请使用getattr(instance, field.name)
。更新:作为 Google Summer of Code 的一部分,Django 贡献者正在开发一个 API 来替换 _Meta 对象。请参阅:
- https://groups.google.com/forum/#!topic/ django-developers/hD4roZq0wyk
- https://code.djangoproject.com/wiki/new_meta_api
_meta
is private, but it's relatively stable. There are efforts to formalise it, document it and remove the underscore, which might happen before 1.3 or 1.4. I imagine effort will be made to ensure things are backwards compatible, because lots of people have been using it anyway.If you're particularly concerned about compatibility, write a function that takes a model and returns the fields. This means if something does change in the future, you only have to change one function.
I believe this will return a list of
Field
objects. To get the value of each field from the instance, usegetattr(instance, field.name)
.Update: Django contributors are working on an API to replace the _Meta object as part of a Google Summer of Code. See:
- https://groups.google.com/forum/#!topic/django-developers/hD4roZq0wyk
- https://code.djangoproject.com/wiki/new_meta_api
我知道这篇文章已经很老了,但我只是想告诉任何正在搜索相同内容的人,有一个公共和官方 API 可以执行此操作:
get_fields()
和get_field( )
用法:
https://docs.djangoproject.com/en/3.2/ref/models/meta/#retriving-all-field-instances-of-a-model
I know this post is pretty old, but I just cared to tell anyone who is searching for the same thing that there is a public and official API to do this:
get_fields()
andget_field()
Usage:
https://docs.djangoproject.com/en/3.2/ref/models/meta/#retrieving-all-field-instances-of-a-model
get_fields()
返回一个tuple
,每个元素都是一个Model field
类型,不能直接用作字符串。因此,field.name
将返回字段名称my_model_fields = [field.name for field in MyModel._meta.get_fields()]
上面的代码将返回一个包含所有字段名称的列表
示例
get_fields()
returns atuple
and each element is aModel field
type, which can't be used directly as a string. So,field.name
will return the field namemy_model_fields = [field.name for field in MyModel._meta.get_fields()]
The above code will return a list containing all field names
Example
现在有特殊方法 - get_fields()
它接受两个参数,可用于控制返回哪些字段:
include_parents
默认为真。递归地包含在父类上定义的字段。如果设置为 False,get_fields() 将仅搜索直接在当前模型上声明的字段。直接从抽象模型或代理类继承的模型中的字段被认为是本地的,而不是父级的。
include_hidden
默认为假。如果设置为 True,get_fields() 将包含用于支持其他字段功能的字段。这还包括任何具有以“+”开头的 related_name 的字段(例如 ManyToManyField 或foreignKey),
Now there is special method - get_fields()
It accepts two parameters that can be used to control which fields are returned:
include_parents
True by default. Recursively includes fields defined on parent classes. If set to False, get_fields() will only search for fields declared directly on the current model. Fields from models that directly inherit from abstract models or proxy classes are considered to be local, not on the parent.
include_hidden
False by default. If set to True, get_fields() will include fields that are used to back other field’s functionality. This will also include any fields that have a related_name (such as ManyToManyField, or ForeignKey) that start with a “+”
fields = [f"{f.name}_id" if f.is_relation else f.name for f in model._meta.fields]
fields = [f"{f.name}_id" if f.is_relation else f.name for f in model._meta.fields]
这是 Django 本身在从模型构建表单时完成的事情。它使用 _meta 属性,但正如 Bernhard 指出的,它同时使用 _meta.fields 和 _meta.many_to_many。查看 django.forms.models.fields_for_model,您可以这样做:
This is something that is done by Django itself when building a form from a model. It is using the _meta attribute, but as Bernhard noted, it uses both _meta.fields and _meta.many_to_many. Looking at django.forms.models.fields_for_model, this is how you could do it:
_meta 包含的模型字段作为相应字段对象的列表列在多个位置。将它们作为字典使用可能更容易,其中键是字段名称。
在我看来,这是收集和组织模型字段对象的最多余且最具表现力的方式:(
参见此示例用法在 django.forms.models.fields_for_model 中。)
The model fields contained by _meta are listed in multiple locations as lists of the respective field objects. It may be easier to work with them as a dictionary where the keys are the field names.
In my opinion, this is most irredundant and expressive way to collect and organize the model field objects:
(See This example usage in django.forms.models.fields_for_model.)
这个怎么样呢。
How about this one.
如果您的管理站点需要此功能,还有
ModelAdmin.get_fields
方法(docs),它返回字段名称字符串的
。列表
例如:
If you need this for your admin site, there is also the
ModelAdmin.get_fields
method (docs), which returns alist
of field namestrings
.For example:
根据 django 文档 2.2,您可以使用:
要获取所有字段:
Model._meta.get_fields()
要获取单个字段:
Model._meta.get_field('field name')< /代码>
例如。
Session._meta.get_field('expire_date')
As per the django documentation 2.2 you can use:
To get all fields:
Model._meta.get_fields()
To get an individual field:
Model._meta.get_field('field name')
ex.
Session._meta.get_field('expire_date')
instance._meta.get_fields()
返回 Django 模型中所有字段(即列)的列表。此方法用于内省模型的字段、它们的类型以及它们与其他模型的关系。该方法返回 Field 对象的列表,这些对象表示模型中的各个字段。
例如,假设您有一个名为
MyModel
的 Django 模型。您可以使用instance._meta.get_fields()
获取模型中所有字段的列表:fields 变量现在将包含
MyModel
中所有字段的列表> 模型,包括id
、name
、created_at
等字段以及任何相关字段(例如外键 )。您可以使用此列表来访问和操作模型中的各个字段。instance._meta.get_fields()
returns a list of all the fields (i.e. columns) in a Django model.This method is used to introspect the model's fields, their types, and their relationships with other models. The method returns a list of Field objects, which represent the individual fields in the model.
For example, suppose you have a Django model called
MyModel
. You can useinstance._meta.get_fields()
to get a list of all the fields in the model:The fields variable will now contain a list of all the fields in the
MyModel
model, including fields such asid
,name
,created_at
, and any related fields (such as foreign keys). You can use this list to access and manipulate the individual fields in the model.为了以简洁易读的方式获取所有字段,您可以使用“model_to_dict”将它们收集到字典中:
To get all fields in a concise and readable manner you can use "model_to_dict" to collect them into a dictionary:
另一种方法是将函数添加到模型中,当您想要覆盖日期时,可以调用该函数。
Another way is add functions to the model and when you want to override the date you can call the function.