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 models
from django.utils import timezone

class 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 model. This model provides a default ordering for QuerySets and an additional created_delta() method. Both models, Content and OrderedContent, operate on the same database table, and objects are accessible via the ORM through either model.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.202