简体   繁体   中英

recursive relationship in django doesn't work

In official documnetation is writting https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

To create a recursive relationship – an object that has a many-to-one relationship with itself – use models.ForeignKey('self').

For example I use the next model:

class MediaGroup:
  name = models.CharField(max_length=200)
  parent = models.ForeignKey('self', blank=True, related_name="children")

and when I run syncdb it throws me the next exception:

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'

你错过了(models.Model)

class MediaGroup(models.Model):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM