How to introspect django model fields?

后端 未结 4 1578
爱一瞬间的悲伤
爱一瞬间的悲伤 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:28

    If you would like to see ALL fields on a Django model object you can simply introspect it by calling ._meta.get_fields() on the class (or an instantiated model object) to get at list of all fields. This API is current with the latest version of Django.

    Example:

    from django.contrib.auth.models import User
    User._meta.get_fields()
    

    This will return a tuple of all model fields. Documentation can be found HERE.

提交回复
热议问题