Can you explain Django contenttypes at a very basic level?
What is their purpose?
ContentType
is just a model and table in the database that contains information about all the other tables/models in your django application.
Postgres table:
=> \d django_content_type;
Column | Type | Modifiers
-----------+------------------------+-----------------------------------------
id | integer | not null ...
name | character varying(100) | not null
app_label | character varying(100) | not null
model | character varying(100) | not null
Data in postgres:
=> SELECT * from django_content_type;
id | name | app_label | model
----+-----------------------+-------------------+---------------------
1 | permission | auth | permission
2 | group | auth | group
3 | user | auth | user
4 | auth user groups | auth | authusergroups
...
For example, if you want to build a custom admin in your application, then to get list of tables you can use the ContentType
model:
>>> from django.contrib.contenttypes.models import ContentType
>>> tables = ContentType.objects.filter(app_label="my_app")
Check the django admin source code for use cases. ContentTypes
is actively used there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With