Django get field's name from Field object
我试图找出从
如果
这似乎是获取模型字段名称的正确方法。当解释从旧API到新型号
MyModel._meta.get_all_field_names() becomes:
1
2
3
4
5
6
7
8 from itertools import chain
list(set(chain.from_iterable(
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
for field in MyModel._meta.get_fields()
# For complete backwards compatibility, you may want to exclude
# GenericForeignKey from the results.
if not (field.many_to_one and field.related_model is None)
)))
号
和
1 [f.name for f in MyModel._meta.get_fields()]
号
另一方面,当你想要一个字段对象时,你可以通过它的名字得到它:
1 | f = MyModel._meta.get_field(name) |
因此,