The contenttypes framework
Tags: django, python
Published: 2022-08-16 03:46:00


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:


```python
from django.contrib.contenttypes.models import ContentType

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

query_post_type

```
```python
The Output : <ContentType: bloge | post>
```

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

```python
ContentType.objects.all()
```
```python
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>>
```

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

```python
query_post_type.model_class()
```
```
The Output : bloge.models.Post
```
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.

```python
from blog.models import Post
ContentType.objects.get_for_model(Post)
```
```
The OutPut
 <ContentType: bloge | post>
```

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.

```python
query_post_type.get_object_for_this_type(pk=1)
```
```
The OutPut : <Post: An Example Post For this section >
```

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

```python
query_post_type.model_class().objects.get(pk=1)
```
```
The OutPut : <Post: An Example Post>
```

```python

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

```

```
The OutPut: True
```

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>
```

![ending_artical](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cmzriu79unrtct7g3hq.gif)