这是代码:
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()
类经销商
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
我在
get_query_set
中覆盖了models.Manager
的CarSourceManager
,显然,它继续递归自称为它。我已经看过Manager的代码,但无法弄清楚,请帮助我。 最佳答案
查看此答案:https://stackoverflow.com/a/18208725/1085511
基本上你不能使用
super(self.__class__, self)
采用
super(CarSourceManager, self)
代替。
相关经理的
self.__class__
与CarSourceManager
不同,因此循环不同。