Search code examples
pythondjangopython-3.xdjango-modelsdjango-queryset

Overriding the Update method of Django queryset


As part of one of the requirement, we are overriding the Update method in the custom Queryset.

Sample code is as follows.

from django.db.models.query import QuerySet

class PollQuerySet(QuerySet):
    def update(self, *args, **kwargs):
        # Some Business Logic

        # Call super to continue the flow -- from below line we are unable to invoke super
        super().update(*args, **kwargs)

class Question(models.Model):
    objects = PollQuerySet.as_manager()

    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

It is unable to invoke update in base Queryset from the Custom Queryset.

TypeError at /polls/ must be type, not PollQuerySet

Any solution is much appreciated.


Solution

  • If I have understood your question correctly, you are unable to call the update method in the super class. if so that's because you are calling it wrong. Here is how:

    super(PollQuerySet,self).update(*args, **kwargs)
    

    In the case of python 3.x the class name and self become optional parameters. So the above line can be shortened to

    super().update(*args, **kwargs)