Home > Blockchain >  Django model with exactly 2 users references
Django model with exactly 2 users references

Time:12-29

I want to define a 'Game' model in which exactly (and not more) 2 users will compete. Each of this user can compete in another game in parallel or later. I thought about a Manytomanyfield, but I don't know how to restrict the number of users. How to do it?

CodePudding user response:

just add two user fields.

class Game(models.Model):
    game_id = models.IntegerField(primary_key=True)
    first_player = models.ForeignKey(User)
    second_player = models.ForeignKey(User)

    winner = models.ForeignKey(User, related_name='winner')

CodePudding user response:

Just create a model with two user fields (player1, player2).

  • Related