Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 328
Srinivas M Avatar asked Feb 20 '17 11:02

Srinivas M


1 Answers

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) 
like image 95
e4c5 Avatar answered Oct 14 '22 07:10

e4c5