前提
django1.10を使っています。
python3.5.1でやっています。
やりたいこと
django1.10では、_meta.get_field_by_nameが使えなくなったのですが、
_meta.get_field_by_nameと同じことができるものってあるんですか?
スタック・オーバーフローはプログラマーとプログラミングに熱心な人のためのQ&Aサイトです。すぐ登録できます。
登録してこのコミュニティに参加するdjango 1.10のModel _meta
APIについてのドキュメントによると、Options.get_field(name)
かOptions.get_fields()
を使って書き換えられる、とのことです。
具体的に_meta.get_field_by_name(name)
についてどうすればよいかも書いてあったので (まだ英語のままですが) 引用します。
Assuming you have a model named
MyModel
, the following substitutions can be made to convert your code to the new API:(中略)
MyModel._meta.get_field_by_name(name)
returns a tuple of these four
values with the following replacements:
field
can be found byMyModel._meta.get_field(name)
model
can be found through themodel
attribute on the field.direct
can be found by:not field.auto_created or field.concrete
Theauto_created
check excludes all “forward” and “reverse” relations that are created by Django, but this also includesAutoField
andOneToOneField
on proxy models. We avoid filtering out these attributes using theconcrete
attribute.m2m
can be found through themany_to_many
attribute on the field.
ただし、新しいAPIを使う形にリファクタリングする方が良いだろう、とも書いてあります。
より詳しくはドキュメントを参考にしてください。
_meta
API -- Django 1.10 Documentation (ja)self._meta.get_fields(***)
とすると、TypeError: Invalid argument for include_parents: kind
と出てしまいます。
get_field(field_name)
ではなくget_fields()
をご使用なのでしょうか。後者はフィールド名の引数をとりません。