41

I have a model with a generic relation:

TrackedItem --- genericrelation ---> any model

I would like to be able to generically get, from the initial model, the tracked item.

I should be able to do it on any model without modifying it.

To do that I need to get the content type and the object id. Getting the object id is easy since I have the model instance, but getting the content type is not: ContentType.object.filter requires the model (which is just content_object.__class__.__name__) and the app_label.

I have no idea of how to get in a reliable way the app in which a model is.

For now I do app = content_object.__module__.split(".")[0], but it doesn't work with django contrib apps.

3 Answers 3

121

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)
4
  • I think @DanielRoseman's answer is better since it doesn't require access to the private member _meta Commented Aug 8, 2019 at 17:05
  • 9
    @AddisonKlinke maybe but to be fair his answer requires an import and a database hit
    – theEpsilon
    Commented Feb 3, 2020 at 21:39
  • 5
    _meta isn't private, it's prefixed to avoid collisions with user-created model fields. It's documented here: docs.djangoproject.com/en/3.2/ref/models/meta Commented Aug 3, 2021 at 14:37
  • IIRC whilst _meta is public, app_label is not (per the Django documentation). That said, I don't recall it changing for years and years, so I happily use it.
    – Kye
    Commented Aug 25, 2021 at 8:06
43

You don't need to get the app or model just to get the contenttype - there's a handy method to do just that:

from django.contrib.contenttypes.models import ContentType

ContentType.objects.get_for_model(myobject)

Despite the name, it works for both model classes and instances.

4
  • 10
    Is it? Doesn't this hit the database?
    – meshy
    Commented Nov 3, 2014 at 16:44
  • 2
    @meshy yes but django ContentType uses a cache on it's manager so it only queries once per model.
    – dalore
    Commented Mar 6, 2016 at 11:05
  • 5
    The import is: from django.contrib.contenttypes.models import ContentType
    – mrmuggles
    Commented Dec 9, 2016 at 0:17
  • 2
    Use .app_label to get the app string from the ContentType object returned by get_for_model() method. I added an example below. Hope to be helpful.
    – Dos
    Commented Nov 22, 2017 at 13:49
9

You can get both app_label and model from your object using the built-in ContentType class:

from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User

user_obj = User.objects.create()
obj_content_type = ContentType.objects.get_for_model(user_obj)

print(obj_content_type.app_label)
# u'auth'
print(obj_content_type.model)
# u'user'

This is better approach respect of using the _meta properties that are defined for private purposes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.