Django: Why do some model fields clash with each other?

后端 未结 6 2308
南笙
南笙 2020-11-29 16:10

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

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


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 16:45

    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.

    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()
    

提交回复
热议问题