在 Django 中添加来自同一模型的两个外键字段
Add two ForeignKey Fields from same Model in Django
我正在尝试在 Django 中创建用户和关注者关系,如下所示
id | user_id | follower_id
1 | 20 | 45
2 | 20 | 53
3 | 32 | 20
为此,我做了以下工作:
class UserFollower(models.Model):
user_id = models.ForeignKey(User)
follower_id = models.ForeignKey(User)
def __str__(self):
return "{} following {}".format(self.follower_id.username, self.user_id.username)
其中 User
是 django.contrib.auth.models.User
模型。在 运行 makemigrations
上,我收到以下错误:
ERRORS:
AppName.UserFollower.follower_id: (fields.E304) Reverse accessor for 'UserFollower.follower_id' clashes with reverse accessor for 'UserFollower.user_id'.
HINT: Add or change a related_name argument to the definition for 'UserFollower.follower_id' or 'UserFollower.user_id'.
AppName.UserFollower.user_id: (fields.E304) Reverse accessor for 'UserFollower.user_id' clashes with reverse accessor for 'UserFollower.follower_id'.
HINT: Add or change a related_name argument to the definition for 'UserFollower.user_id' or 'UserFollower.follower_id'.
我的问题是,为什么这是错误的?我该如何解决这个问题?
您需要添加related_name
class UserFollower(models.Model):
user_id = models.ForeignKey(User,related_name="users")
follower_id = models.ForeignKey(User,related_name="followers")
这是为什么
"If a model has a ForeignKey, instances of the foreign-key model will
have access to a Manager that returns all instances of the first
model. By default, this Manager is named FOO_set, where FOO is the
source model name, lowercased."
But if you have more than one foreign key in a model, django is unable
to generate unique names for foreign-key manager. You can help out by
adding "related_name" arguments to the foreignkey field definitions in
your models.
因此,您可以在 django 中阅读更多内容 docs
我正在尝试在 Django 中创建用户和关注者关系,如下所示
id | user_id | follower_id
1 | 20 | 45
2 | 20 | 53
3 | 32 | 20
为此,我做了以下工作:
class UserFollower(models.Model):
user_id = models.ForeignKey(User)
follower_id = models.ForeignKey(User)
def __str__(self):
return "{} following {}".format(self.follower_id.username, self.user_id.username)
其中 User
是 django.contrib.auth.models.User
模型。在 运行 makemigrations
上,我收到以下错误:
ERRORS:
AppName.UserFollower.follower_id: (fields.E304) Reverse accessor for 'UserFollower.follower_id' clashes with reverse accessor for 'UserFollower.user_id'.
HINT: Add or change a related_name argument to the definition for 'UserFollower.follower_id' or 'UserFollower.user_id'.
AppName.UserFollower.user_id: (fields.E304) Reverse accessor for 'UserFollower.user_id' clashes with reverse accessor for 'UserFollower.follower_id'.
HINT: Add or change a related_name argument to the definition for 'UserFollower.user_id' or 'UserFollower.follower_id'.
我的问题是,为什么这是错误的?我该如何解决这个问题?
您需要添加related_name
class UserFollower(models.Model):
user_id = models.ForeignKey(User,related_name="users")
follower_id = models.ForeignKey(User,related_name="followers")
这是为什么
"If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased."
But if you have more than one foreign key in a model, django is unable to generate unique names for foreign-key manager. You can help out by adding "related_name" arguments to the foreignkey field definitions in your models.
因此,您可以在 django 中阅读更多内容 docs