1

Here's the code:

class 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

class Dealer(models.Model):
    ...

class 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')

When I execute this:

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

It occur error as below:

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

I overwrite the get_query_set of models.Manager in CarSourceManager, obviously, it continue call it self recursively. I've watched the code of Manager but can't figure it out, please help me.

weidwonder
  • 122
  • 1
  • 7

2 Answers2

2

See this answer: https://stackoverflow.com/a/18208725/1085511

Basically you can not use

super(self.__class__, self)

Use

super(CarSourceManager, self)

instead.

The related manager's self.__class__ is different from CarSourceManager therefore the loop.

Community
  • 1
  • 1
Krzysztof Szularz
  • 5,151
  • 24
  • 35
  • haaa, it works!! thx very much, but I'm new to stackoverflow, have no reputation, can't upvote ur answer. Sorry about that. seems that I still need to learn python hard.;) thank u again! – weidwonder Aug 14 '15 at 09:20
0

This is exactly why you have to name the class explicitly when you call super:

return super(CarSourceManager, self).get_query_set().filter(status='S')
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Yeah! thank u, u are right. I'm new to stackoverflow, have no reputation, can't upvote ur answer. Sorry about that. I will upvote later. – weidwonder Aug 14 '15 at 09:22