在官方文档中写的是https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
要创建递归关系(与自身具有多对一关系的对象),请使用 models.ForeignKey('self')。
例如我使用下一个模型:
class MediaGroup:
name = models.CharField(max_length=200)
parent = models.ForeignKey('self', blank=True, related_name="children")
当我运行 syncdb 时,它会抛出下一个异常:
File "/usr/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 939, in __init__
assert isinstance(to, basestring), "%s(%r) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string %r" % (self.__class__.__name__, to, RECURSIVE_RELATIONSHIP_CONSTANT)
AssertionError: ForeignKey(<class webpanel.models.MediaGroup at 0x225ca10>) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'