我想在django为我的学校创建一个善良的社交网络。 我的问题是我创建了一个跟随函数,django给了我一个错误响应。 我该怎么处理?
django代码:
class User(User):
joined = models.DateTimeField(auto_now_add=True)
is_activated = models.BooleanField(default=False)
def __unicode__(self):
return self.username
class Following_User(models.Model):
user_id = models.ForeignKey(User)
user_follow = models.ForeignKey(User)
并且是错误响应:
ERRORS:
cherry.Following_User.user_follow: (fields.E304) Reverse accessor for 'Following_User.user_follow' clashes with reverse accessor for 'Following_User.user_id'.
HINT: Add or change a related_name argument to the definition for 'Following_User.user_follow' or 'Following_User.user_id'.
cherry.Following_User.user_id: (fields.E304) Reverse accessor for 'Following_User.user_id' clashes with reverse accessor for 'Following_User.user_follow'.
HINT: Add or change a related_name argument to the definition for 'Following_User.user_id' or 'Following_User.user_follow'.
System check identified 2 issues (0 silenced).
答案 0 :(得分:0)
如果您的模型包含两个字段作为另一个模型的外键,则需要为每个字段指定related_name
。
class Following_User(models.Model):
user_id = models.ForeignKey(User, related_name="following_user_id")
user_follow = models.ForeignKey(User, related_name="following_user_follow")
https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ForeignKey.related_name
拥有related_name
的原因是,如果要从外键查询,django会给你反向关系。对于与当前模型有关系的单个字段,django会为您提供一个默认值blahfield_set
,但在这种情况下,related_name
将用于消除歧义。