Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the diffrence between django apps and django contenttype?

Tags:

python

django

What is the diffrence between django apps and django contenttype in getting models for example

from django.apps import apps
model = apps.get_model(app_label=app_label,model_name=model_name)

and

from django.contrib.contenttypes.models import ContentType
model = ContentType.objects.get(app_label=app_label, model = model).model_class()
like image 880
Mohammad Javad Shamloo Avatar asked Oct 27 '25 12:10

Mohammad Javad Shamloo


1 Answers

The apps.get_model(…) will look for the model stored in the AppConfig that corresponds with the app_name. Indeed, if we look at the source code [GitHub], we see:

def get_model(self, app_label, model_name=None, require_ready=True):
    # …
    if require_ready:
        self.check_models_ready()
    else:
        self.check_apps_ready()

    if model_name is None:
        app_label, model_name = app_label.split('.')

    app_config = self.get_app_config(app_label)

    if not require_ready and app_config.models is None:
        app_config.import_models()

    return app_config.get_model(model_name, require_ready=require_ready)

It thus first determines the AppConfig that corresponds with the app_label, and then makes a call to get the model object. The AppConfig has a list where it stores all the models for that App. Then the get_model(…) logic of the AppConfig is used [GitHub]:

def get_model(self, model_name, require_ready=True):
    # …
    if require_ready:
        self.apps.check_models_ready()
    else:
        self.apps.check_apps_ready()
    try:
        return self.models[model_name.lower()]
    except KeyError:
        raise LookupError(
            "App '%s' doesn't have a '%s' model." % (self.label, model_name))

It thus will look for a model in the dictionary and return a reference to that model object.


If you work with the ContentType, you actually first make a database query to fetch the corresponding ContentType. You can query the ContentType by id for example (which is the main purpose of a content type: mapping a primary key to a model).

Once the ContentType is retrieved, Django knows the name of the app and the name of the model. It then will make a call to the apps.get_model(…) that we discussed earlier, and thus retrieve a reference to the correct model. Indeed, the .model_class() method is implemented as [GitHub]:

def model_class(self):
    # …
    try:
        return apps.get_model(self.app_label, self.model)
    except LookupError:
        return None

If you thus know the app name and the model name, it makes no sense to work with the ContentType. Especially since the database can run behind on the content types, and thus return the wrong one. The ContentType model is usually used to refer to a specific model in the database. For example by using a GenericForeignKey [Django-doc] which consists out of a pair of two fields: one ForeignKey that links to an item of the ContentType, and one with the primary key of the object referred to. If you access the GenericForeignKey of a model object, it will first retrieve the ContentType of that object, and then make a query on that model with the primary key of the record object.

like image 102
Willem Van Onsem Avatar answered Oct 29 '25 02:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!