How to introspect django model fields?

后端 未结 4 1576
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 17:01

I am trying to obtain class information on a field inside a model, when I only know name of the field and name of the model (both plain strings). How is it possible?

4条回答
  •  半阙折子戏
    2020-12-07 17:34

    The answer from Anurag Uniyal to use get_field_by_name is now (5 years later) outdated as get_field_by_name is deprecated. Django will give you the following hint:

    RemovedInDjango110Warning: 'get_field_by_name is an unofficial API that has been deprecated. You may be able to replace it with 'get_field()'

    API docs for get_field are here.

提交回复
热议问题