DEV Community

Cover image for The contenttypes framework
Abdelrahman hassan hamdy
Abdelrahman hassan hamdy

Posted on • Edited on

The contenttypes framework

The main use of the contenttypes framework is to dynamically load models classes based on their name, and subsequently, query objects for that model.

The main model is ContentType, which is importable from django.contrib.contenttypes.model. This works like a normal model whose objects you can query.

If you know the model you want, you can query it using the app name and model name.

Entering Shell Commands:



from django.contrib.contenttypes.models import ContentType

query_post_type = ContentType.objects.get(app_label="bloge", model="post")

query_post_type



Enter fullscreen mode Exit fullscreen mode


The Output : <ContentType: bloge | post>


Enter fullscreen mode Exit fullscreen mode

We could also get a list of all the ContentTypes we have installed, by calling ContentType.objects.all():



ContentType.objects.all()


Enter fullscreen mode Exit fullscreen mode


The Output : 

<QuerySet [<ContentType: admin | log entry>, <ContentType: auth | permission>, <ContentType: auth | group>, <ContentType: auth | user>, <ContentType: contenttypes | content type>, <ContentType: sessions | session>, <ContentType: bloge | post>>


Enter fullscreen mode Exit fullscreen mode

Note that the ContentType object is not the Post model. But, we can retrieve the class with the model_class() method:



query_post_type.model_class()


Enter fullscreen mode Exit fullscreen mode


The Output : bloge.models.Post


Enter fullscreen mode Exit fullscreen mode

It’s also possible to go in reverse, and retrieve the ContentType object from the model. This is done with the ContentType.objects.get_for_model() method.



from blog.models import Post
ContentType.objects.get_for_model(Post)


Enter fullscreen mode Exit fullscreen mode


The OutPut
 <ContentType: bloge | post>


Enter fullscreen mode Exit fullscreen mode

This is useful if you want to know the app_label and model name for a model class.

Once a ContentType object is found, the shortcut method get_objects_for_this_type() will perform a get lookup and retrieve objects for that model class.



query_post_type.get_object_for_this_type(pk=1)


Enter fullscreen mode Exit fullscreen mode


The OutPut : <Post: An Example Post For this section >


Enter fullscreen mode Exit fullscreen mode

Note that this is a shortcut to get on the Post.objects manager instance:



query_post_type.model_class().objects.get(pk=1)


Enter fullscreen mode Exit fullscreen mode


The OutPut : <Post: An Example Post>


Enter fullscreen mode Exit fullscreen mode



query_post_type.get_object_for_this_type(pk=1) == query_post_type.model_class().objects.get(pk=1)



Enter fullscreen mode Exit fullscreen mode


The OutPut: True


Enter fullscreen mode Exit fullscreen mode

Other methods of loading objects (like filter() and all()) are similarly available – remember once you call model_class() it’s just like you’ve imported the model.

This is the extent of what is generally needed to be known to use contenttypes, but if you’re curious about the extra methods that are available, then you can read the contenttypes framework documentation.

Now that we’ve seen how the contenttypes framework can let us dynamically access models.

Another quick example to see the differences between two ways



c1 = ContentType.objects.get_for_model(Post)
c1 
Out[21]: <ContentType: blog | post>
c2 = ContentType.objects.get(app_label="blog", model="post")
c2
Out[23]: <ContentType: blog | post>
c1.get_object_for_this_type()
Out[24]: <Post: hello world#@@ eee>
c2.model_class().objects.first()
Out[25]: <Post: hello world#@@ eee>


Enter fullscreen mode Exit fullscreen mode

ending_artical

Top comments (0)