How to get the app a Django model is from?

后端 未结 3 1981
既然无缘
既然无缘 2021-01-01 09:47

I have a model with a generic relation:

TrackedItem --- genericrelation ---> any model

I would like to be able to generically get, from

3条回答
  •  Happy的楠姐
    2021-01-01 09:53

    The app_label is available as an attribute on the _meta attribute of any model.

    from django.contrib.auth.models import User
    print User._meta.app_label
    # The object name is also available
    print User._meta.object_name
    

提交回复
热议问题