Django执行向后关系查询时,在使用管理器时发生最大递归深度超出错误

2024-04-24 04:04:40 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

类CarSource

class CarSource(models.Model):
    status = models.CharField(max_length=1, blank=True, null=True)
    dealer = models.ForeignKey(Dealer, blank=True, null=True, \
                  on_delete=models.SET_NULL, related_name='cars', \
                  verbose_name=u'dealer own this car')
    objects = CarSourceManager()

等级经销商

^{pr2}$

CarSourceManager类:

class CarSourceManager(models.Manager):
    ''' Manage query in CarSource, filter data that was processed successfully.'''
    def get_query_set(self):
        return super(self.__class__, self).get_query_set().filter(status='S')

当我执行此操作时:

from ... import Dealer
d = Dealer.objects.get(id = 2)
d.cars.all()

出现如下错误:

File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
File "/...path..of..error..file../apps/car/managers.py", line 9, in all
return super(self.__class__, self).all().filter(status='S')
RuntimeError: maximum recursion depth exceeded while calling a Python object

我重写了models.Managermodels.Managerget_query_set,显然,它继续自递归地调用它。我看了经理守则,但搞不懂,请帮帮我。在


Tags: ofpathinselfreturnmodelsstatuserror
2条回答

这正是调用super时必须显式命名类的原因:

return super(CarSourceManager, self).get_query_set().filter(status='S')

看到这个答案:https://stackoverflow.com/a/18208725/1085511

基本上你不能用

super(self.__class__, self)

使用

^{pr2}$

相反。在

相关管理器的self.__class__CarSourceManager不同,因此是循环。在

相关问题 更多 >