Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django model with exactly 2 users references

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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading