创建时Django模型超出了最大递归深度

时间:2013-07-12 10:56:52

标签: django recursion model django-orm

我的Django模型之一存在这个奇怪的问题,我能够解决它,但不明白发生了什么。

这些是模型:

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()])

每当我创建一个新的(空的)Team对象并希望从中获取players时,我得到了RuntimeError: maximum recursion depth exceeded。 例如:

>>> 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()])
...

为什么会这样?我能够通过检查pk来修复它,然后只生成名称,但我认为它应该起作用的方式只返回名称,因为", ".join...将是一个空列表。相反,发生了一些我不理解的递归。

1 个答案:

答案 0 :(得分:18)

问题是,当team.players实例尚未保存到数据库时,您无法访问Team字段。尝试这样做会引发ValueError

但是,在尝试引发此ValueError时,代码将尝试获取将team对象的表示,该对象将间接调用unicode(team)。这将尝试访问self.players,它会在第一个引发之前尝试引发另一个ValueError。这一直持续到达到最大递归深度,但仍然没有抛出ValueError。因此,您只会看到RuntimeError

如果您执行以下任一操作,那么(应该?)会发生同样的情况:

>>> team
>>> repr(team)
>>> unicode(team)