代码如下:
类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.Manager
中models.Manager
的get_query_set
,显然,它继续自递归地调用它。我看了经理守则,但搞不懂,请帮帮我。在
这正是调用super时必须显式命名类的原因:
看到这个答案:https://stackoverflow.com/a/18208725/1085511
基本上你不能用
使用
^{pr2}$相反。在
相关管理器的
self.__class__
与CarSourceManager
不同,因此是循环。在相关问题 更多 >
编程相关推荐