gpt4 book ai didi

python - 当我使用 Manager 时,Django 执行向后关系查询发生最大递归深度超出错误

转载 作者:行者123 更新时间:2023-12-01 04:34:10 25 4
gpt4 key购买 nike

代码如下:

汽车源类

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

我覆盖了CarSourceManagermodels.Managerget_query_set,显然,它继续递归地调用它自己。我看了Manager的代码,但无法理解,请帮助我。

最佳答案

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

基本上不能用

super(self.__class__, self)

使用

super(CarSourceManager, self)

相反。

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

关于python - 当我使用 Manager 时,Django 执行向后关系查询发生最大递归深度超出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32004899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com