python - Django 用户 M2M 关系

标签 python django django-models

尝试使用以下模型同步数据库时:

class Contact(models.Model):
    user_from = models.ForeignKey(User,related_name='from_user')
    user_to = models.ForeignKey(User, related_name='to_user')

    class Meta:
        unique_together = (('user_from', 'user_to'),)

User.add_to_class('following', models.ManyToManyField('self', through=Contact, related_name='followers', symmetrical=False))

我收到以下错误:

Error: One or more models did not validate:
auth.user: Accessor for m2m field 'following' clashes with related m2m field 'User.followers'. Add a related_name argument to the definition for 'following'.

auth.user: Reverse query name for m2m field 'following' clashes with related m2m field 'User.followers'. Add a related_name argument to the definition for 'following'.
auth.user: The model User has two manually-defined m2m relations through the model Contact, which is not permitted. Please consider using an extra field on your intermediary model instead.

auth.user: Accessor for m2m field 'following' clashes with related m2m field 'User.followers'. Add a related_name argument to the definition for 'following'.

auth.user: Reverse query name for m2m field 'following' clashes with related m2m field 'User.followers'. Add a related_name argument to the definition for 'following'.

最佳答案

请参阅 Carl 对此问题的回答 Django: Model name clash

关于python - Django 用户 M2M 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2592187/

相关文章:

django - 'ManyToManyField' 对象没有属性 'm2m_reverse_field_name'

python - 生成集合中所有大小相等的分区

python - with 语句表达式中引发的异常会怎样?

python - 计算 Python 中 GQL 查询给出的不同字符串的数量

python - 添加时间戳和用户名到日志

python - 每当我尝试添加 <int :pk> in urls. py 时,在 Django 中找不到页面 (404)

python - 为什么 pandas.read_excel 不能运行?

django - 在开发、暂存和生产之间分离自定义设置变量

python - 克隆 Django 模型或在第二个模型中添加区分字段?

django - 按查询排序