Python Django在执行向后关系查询时,在使用管理器时发生了超过最大递归深度的错误
代码如下: 类车源Python Django在执行向后关系查询时,在使用管理器时发生了超过最大递归深度的错误,python,django,recursion,model,Python,Django,Recursion,Model,代码如下: 类车源 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', \
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()
等级经销商
class Dealer(models.Model):
...
类别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
我覆盖了CarSourceManager
中models.Manager
的get\u query\u set
,显然,它继续以递归方式调用它。我看了经理的代码,但没看懂,请帮帮我。看这个答案:
基本上你不能用
super(self.__class__, self)
使用
相反
相关管理者的
自我。\uuuu class\uuuu
不同于CarSourceManager
,因此循环。这正是调用super时必须显式命名类的原因:
return super(CarSourceManager, self).get_query_set().filter(status='S')
顺便说一下,我的django版本是1.4.2haaa,它可以工作!!非常感谢,但我是stackoverflow的新手,没有声誉,无法对你的答案进行投票。很抱歉。看来我还需要努力学习python再次感谢你!是 啊谢谢你,你说得对。我对stackoverflow不熟悉,没有声誉,无法投票给你的答案。很抱歉。我稍后会投票。
return super(CarSourceManager, self).get_query_set().filter(status='S')