具有外键的Django模型和同一模型的多个关系

具有外键的Django模型和同一模型的多个关系,django,django-models,foreign-keys,many-to-many,Django,Django Models,Foreign Keys,Many To Many,我有一个django模型,如下所示: class Subscription(models.Model): Transaction = models.ManyToManyField(Transaction, blank=True, null=True) User = models.ForeignKey(User) ...etc... SubUsers = models.ManyToManyField(User, blank=True, null=True) 我正

我有一个django模型,如下所示:

class Subscription(models.Model):
    Transaction = models.ManyToManyField(Transaction, blank=True, null=True)
    User = models.ForeignKey(User)
    ...etc...
    SubUsers = models.ManyToManyField(User, blank=True, null=True)
我正在尝试向用户模型添加一个多个字段,如下所示:

class Subscription(models.Model):
    Transaction = models.ManyToManyField(Transaction, blank=True, null=True)
    User = models.ForeignKey(User)
    ...etc...
    SubUsers = models.ManyToManyField(User, blank=True, null=True)
但我在运行syncdb时出现以下错误:

AssertionError: ManyToManyField(<django.db.models.fields.related.ForeignKey object at 0x19ddfd0>) is invalid. First parameter to ManyToManyField must be either a model, a model name, or the string 'self'

我知道用户模型已正确导入。你知道为什么有两个字段指向用户模型会导致问题吗?提前感谢…

之所以失败,是因为字段名与类名(用户)相同。使用小写字段名,这是Django和Python中的标准约定。看

此外,您还需要在关系中添加一个
相关的\u name
参数:

class Subscription(models.Model):
    user = models.ForeignKey(User)
    sub_users = models.ManyToManyField(User, blank=True, null=True, related_name="subscriptions")