Sri*_*s M 5 python django django-models django-queryset python-3.x
作为其中一项要求的一部分,我们将覆盖自定义Queryset中的Update方法.
示例代码如下.
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')
Run Code Online (Sandbox Code Playgroud)
它无法从自定义查询集调用基本Queryset中的更新.
/ polls /中的TypeError必须是type,而不是PollQuerySet
非常感谢任何解决方案.
如果我已正确理解您的问题,则无法在超类中调用更新方法.如果是这样,那是因为你说错了.方法如下:
super(PollQuerySet,self).update(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
在python 3.x的情况下,类名和self成为可选参数.所以上面的这一行可以缩短为
super().update(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)