Srinivas M Srinivas M -4 years ago 169
Python Question

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(self, 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.

Answer Source

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) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download