Overriding Django Model behaviour with Proxy Model

Reading Time : ~ .

The main Usage of a proxy model is to override the main functionality of existing Model. It is a type of model inheritance without creating a new table in Database. It always query on original model with overridden methods or managers.

You can query, delete, edit, create etc on Proxy model but the effects will be on the original model.

You can define models as a proxy just by adding proxy = True in Meta class of model.

 

# Original Model

class Employee(models.Model):

    EmployeeType = (

        ("D", "Developer"),

        ("M", "Manager"),

    )

    name = models.CharField(max_length=55)

    type = models.CharField(choices=EmployeeType, max_length=1, default='D')

    def __str__(self):

        return self.name

# Model manager to use in Proxy model

class ManagerEmpManager(models.Manager):

    def get_queryset(self):

        return super(ManagerEmpManager, self).get_queryset().filter(

            type='M')

    def create(self, **kwargs):

        kwargs.update({'type': 'M'})

        return super(ManagerEmpManager, self).create(**kwargs)

# Proxy Model

class ManagerEmployee(Employee):

    objects = ManagerEmpManager()

    class Meta:

        proxy = True

In ManagerEmployee I have overridden create method to set type as M(or Manager). whenever you create an object for ManagerEmployee model the type will be set to 'M' and a new object will be created for Employee table. Whenever you query on ManagerEmpManager you will get all employees who are Managers(or type field as 'M'). 

In [1]: from AppName.models import Employee, ManagerEmployee

In [2]: e1 = Employee.objects.create(name="Monkey")

In [3]: e1

Out[3]: <Employee: Monkey>   # Employee object created with type 'D'

In [4]: e1.type

Out[4]: u'D'

In [2]: e2 = ManagerEmployee.objects.create(name="Cow")

In [6]: e2

Out[6]: <ManagerEmployee: Cow>    # object created with type 'M' for model ManagerEmployee(original model Employee)

In [3]: e2.type

Out[3]: u'M'

In [4]: ManagerEmployee.objects.all()    # To get all ManagerEmployee objects(or type 'M') as we defined in ManagerEmpManager class.

Out[4]: <QuerySet [<ManagerEmployee: Cow>]>
In [5]: Employee.objects.all()

Out[5]: <QuerySet [<Employee: Monkey>, <Employee: Cow>]>

Register ManagerEmployee model in admin.py file to get a separate interface for ManagersEmployee model.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Export html web page to pdf using jspdf Chaitanya Kattineni

jsPDF is used to generate pdf files in client-side Javascript. You can find the links for jsPDF here and also you can find the link ...

Continue Reading...
Django Conditional Expressions in Queries Chaitanya Kattineni

Reduce database queries in django with Conditional Expressions. By using Conditional Expressions we can use "If...Elif...Else" expressions while querying the database so that we can ...

Continue Reading...
Integration Of GitHub API with python django Nikhila Mergu

Using Github integration, we can get the user verified email id, general information, git hub URL, id, disk usage, public, private repo's, gists and followers, ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties