class ContentTypeManager
ContentType also has a custom manager, ContentTypeManager, which adds the following methods:
-
clear_cache() -
Clears an internal cache used by
ContentTypeto keep track of models for which it has createdContentTypeinstances. You probably won’t ever need to call this method yourself; Django will call it automatically when it’s needed.
-
get_for_id(id) -
Lookup a
ContentTypeby ID. Since this method uses the same shared cache asget_for_model(), it’s preferred to use this method over the usualContentType.objects.get(pk=id)
-
get_for_model(model, for_concrete_model=True) -
Takes either a model class or an instance of a model, and returns the
ContentTypeinstance representing that model.for_concrete_model=Falseallows fetching theContentTypeof a proxy model.
-
get_for_models(*models, for_concrete_models=True) -
Takes a variadic number of model classes, and returns a dictionary mapping the model classes to the
ContentTypeinstances representing them.for_concrete_models=Falseallows fetching theContentTypeof proxy models.
-
get_by_natural_key(app_label, model) -
Returns the
ContentTypeinstance uniquely identified by the given application label and model name. The primary purpose of this method is to allowContentTypeobjects to be referenced via a natural key during deserialization.
The get_for_model() method is especially useful when you know you need to work with a ContentType but don’t want to go to the trouble of obtaining the model’s metadata to perform a manual lookup:
>>> from django.contrib.auth.models import User >>> ContentType.objects.get_for_model(User) <ContentType: user>
Please login to continue.