Proxy models

Proxy models are used to change the behavior of a model, for example, by including additional methods or different meta options. Both models operate on the database table of the original model. To create a proxy model, add proxy=True to the Meta class of the model.

The following example illustrates how to create a proxy model:

from django.db import modelsfrom django.utils import timezoneclass BaseContent(models.Model):    title = models.CharField(max_length=100)    created = models.DateTimeField(auto_now_add=True)class OrderedContent(BaseContent):    class Meta:        proxy = True        ordering = ['created']    def created_delta(self):        return timezone.now() - self.created

Here, we define an OrderedContent model that is a proxy model for the Content ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.