Maximum recursion depth exceeded on Django model when creating

I have this strange problem with one of my Django models, and I was able to fix it, but I don’t understand what is happening.

These are the models:

class Player(models.Model): facebook_name = models.CharField(max_length=100) nickname = models.CharField(max_length=40, blank=True) def __unicode__(self): return self.nickname if self.nickname else self.facebook_name class Team(models.Model): name = models.CharField(max_length=50, blank=True) players = models.ManyToManyField(Player) def __unicode__(self): name = '(' + self.name + ') ' if self.name else '' return name + ", ".join([unicode(player) for player in self.players.all()]) 

Whenever I create a new (empty) Team object and want to get players from it, I get RuntimeError: maximum recursion depth exceeded . For example:

 >>> team = Team() >>> team.players Traceback (most recent call last): File "<console>", line 1, in <module> File "/Users/walkman/Projects/fociadmin/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 897, in __get__ through=self.field.rel.through, File "/Users/walkman/Projects/fociadmin/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 586, in __init__ (instance, source_field_name)) File "/Users/walkman/Projects/fociadmin/venv/lib/python2.7/site-packages/django/db/models/base.py", line 421, in __repr__ u = six.text_type(self) File "/Users/walkman/Projects/fociadmin/fociadmin/models.py", line 69, in __unicode__ return name + ", ".join([unicode(player) for player in self.players.all()]) File "/Users/walkman/Projects/fociadmin/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 897, in __get__ through=self.field.rel.through, File "/Users/walkman/Projects/fociadmin/venv/lib/python2.7/site-packages/django/db/models/fields/related.py", line 586, in __init__ (instance, source_field_name)) File "/Users/walkman/Projects/fociadmin/venv/lib/python2.7/site-packages/django/db/models/base.py", line 421, in __repr__ u = six.text_type(self) File "/Users/walkman/Projects/fociadmin/fociadmin/models.py", line 69, in __unicode__ return name + ", ".join([unicode(player) for player in self.players.all()]) ... 

Why is this happening? I was able to fix this by checking pk and only generating the name, but what, in my opinion, should work, returns only the name, because ", ".join... will be an empty list. Instead, there is some recursion that I don't understand.

+7
django recursion django-orm model
source share
1 answer

The problem is that you cannot access the team.players field if the Team instance is not already stored in the database. Attempting to do this raises a ValueError .

However, trying to raise this ValueError , the code will try to get a representation of your Team object, which indirectly calls unicode(team) . This will try to access self.players , which will try to raise another ValueError before the first one is raised. This continues until the maximum recursion depth is reached, but there is still no ValueError . Therefore, you will only see RuntimeError .

The same thing would happen (if?) If you did one of the following:

 >>> team >>> repr(team) >>> unicode(team) 
+15
source share

All Articles