26
loading...
This website collects cookies to deliver better user experience
contenttypes
is a Django app that can track all of the models installed in our Django-powered project, providing a high-level, generic interface for working with our models.INSTALLED_APPS
we can see, that django.contrib.contenttypes
is readily present.INSTALLED_APPS = [
...
'django.contrib.contenttypes',
...
]
contenttypes
is just like any other app, it also has migrations and, and views.py. The most important model of the contenttypes app is ContentType
. Let's see what fields ContentType has.class ContentType(models.Model):
app_label = models.CharField(max_length=100)
model = models.CharField(_('python model class name'), max_length=100)
objects = ContentTypeManager()
class Meta:
verbose_name = _('content type')
verbose_name_plural = _('content types')
db_table = 'django_content_type'
unique_together = [['app_label', 'model']]
def __str__(self):
return self.app_labeled_name
app_label
and model
both as string fields, which also are unique_together
.ContentType
is a model that tracks all the models in your Django app and therefore, as soon as we create a new table in the database, a new entry gets created for the new table in the ContentType table. Consequently, ContentType
model objects "points" to a particular table in your Django app. Let's see few entries in the ContentType table.>>> from django.contrib.contenttypes.models import ContentType
>>> ContentType.objects.filter(model='user').values()
<QuerySet [{'id': 4, 'app_label': 'auth', 'model': 'user'}]>
>>> ContentType.objects.filter(model='contenttype').values()
<QuerySet [{'id': 5, 'app_label': 'contenttypes', 'model': 'contenttype'}]>
ContentType
itself is a model, the ContentType model also tracks itself and thus has an entry for itself, this sounds a bit confusing but makes complete sense.model_class
and get_object_for_this_type
, let's see with an example what these methods do.>>> from django.contrib.contenttypes.models import ContentType
>>> user_ct = ContentType.objects.get(model='user')
>>> user_ct.model_class()
django.contrib.auth.models.User
>>> user_ct.get_object_for_this_type(username='bob')
<User: bob>
ContentType
model and contenttypes
framework, in the next article, we will see one of the implementations of ContentType
which is GenericForeignKey
.