Designing a model structure for django

I'm trying to create a model for an application that allows two people to bet with each other (I know that sounds silly ...). I'm wondering how to set a bid with users. Structure is like this

|-------------|       |----------|
|    Bet      |       | User     |
| BetUser1    |       |----------|
| BetUser2    |
| Winner      |
| ...         |
|-------------|

So, we have 2 people who make bets with each other (both from Usersthe django auth system), and then, after one of them wins, there is a winner. Now all these 3 fields are of type User, but:

  • Should I create separate BetUser1 and BetUser2 fields or create some two-two relationships here? (with many-two-many-to-many and with some external way of providing no more than 2 Userscan be assigned to each bet? Winner
  • maybe only user 1 or user 2, of course, no one. How do I create this field, one more ForeignKey(User)or some other?

We are just looking for a new point of view, since it seems that in such a stupid case, I was stuck with a django model model.

+3
source share
2 answers

I would probably add a third model to represent the specific bet that someone placed, since it can be assumed that more than two people can enter the bet. It will look something like this:

USER        WAGER              BET
             User (FK(User))    Description
             Bet  (FK(Bet))     Winner (FK (Wager), null=True)
             Amount

Django user.wager_set bet.wager_set . , . unique_together User Bet Wager, .

, bet.winner.

related_name, Bet Wager Wager Bet. , related_name=wagers Wager.bet.

+4

, " " (, , ...)

chaper Django .

.

+1

Source: https://habr.com/ru/post/1698966/


All Articles