contenttypes.models.ContentType

class ContentType

Each instance of ContentType has two fields which, taken together, uniquely describe an installed model:

app_label

The name of the application the model is part of. This is taken from the app_label attribute of the model, and includes only the last part of the application’s Python import path; “django.contrib.contenttypes”, for example, becomes an app_label of “contenttypes”.

model

The name of the model class.

Additionally, the following property is available:

name

The human-readable name of the content type. This is taken from the verbose_name attribute of the model.

Let’s look at an example to see how this works. If you already have the contenttypes application installed, and then add the sites application to your INSTALLED_APPS setting and run manage.py migrate to install it, the model django.contrib.sites.models.Site will be installed into your database. Along with it a new instance of ContentType will be created with the following values:

  • app_label will be set to 'sites' (the last part of the Python path “django.contrib.sites”).
  • model will be set to 'site'.
doc_Django
2016-10-09 18:34:38
Comments
Leave a Comment

Please login to continue.