Django:为什么一些模型领域相互冲突? [英] Django: Why do some model fields clash with each other?

查看:218
本文介绍了Django:为什么一些模型领域相互冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含2个用户的链接的对象。例如:

I want to create an object that contains 2 links to Users. For example:

class GameClaim(models.Model):
    target = models.ForeignKey(User)
    claimer = models.ForeignKey(User)
    isAccepted = models.BooleanField()

运行服务器时出现以下错误:

but I am getting the following errors when running the server:



字段'target'冲突的访问者相关字段User.gameclaim_set。在target的定义中添加一个related_name参数。

Accessor for field 'target' clashes with related field 'User.gameclaim_set'. Add a related_name argument to the definition for 'target'.



  • '与相关字段'User.gameclaim_set'冲突。在claimer的定义中添加一个related_name参数。

    Accessor for field 'claimer' clashes with related field 'User.gameclaim_set'. Add a related_name argument to the definition for 'claimer'.


  • 解释为什么我得到错误和如何解决它们?

    Can you please explain why I am getting the errors and how to fix them?

    推荐答案

    你有两个外键用户。 Django会自动创建一个从用户返回到GameClaim的反向关系,通常是 gameclaim_set 。但是,由于您有两个FK,因此您将拥有两个 gameclaim_set 属性,这显然是不可能的。所以你需要告诉Django用于反向关系的名字。

    You have two foreign keys to User. Django automatically creates a reverse relation from User back to GameClaim, which is usually gameclaim_set. However, because you have two FKs, you would have two gameclaim_set attributes, which is obviously impossible. So you need to tell Django what name to use for the reverse relation.

    使用FK中的 related_name 属性定义。例如

    Use the related_name attribute in the FK definition. e.g.

    class GameClaim(models.Model):
        target = models.ForeignKey(User, related_name='gameclaim_targets')
        claimer = models.ForeignKey(User, related_name='gameclaim_users')
        isAccepted = models.BooleanField()
    

    这篇关于Django:为什么一些模型领域相互冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆