I’m trying to design a model for a application allowing 2 people to bet with each other (I know, sounds stupid…). What I’m wondering about is how to connect the bet with users. The structure is like this
|-------------| |----------| | Bet | | User | | BetUser1 | |----------| | BetUser2 | | Winner | | ... | |-------------|
So we got 2 people that bet with each other (both are Users from django auth system) and then, after one of them wins, there’s a winner. Now all those 3 fields are of type User, but:
- Should I make BetUser1 and BetUser2 separate fields, or design some many-to-two relationship here? (with many-to-two being a many-to-many and with some external way of ensuring no more then 2
Userscan be assigned to each bet? - winner can only be either user 1 or user 2, noone else of course. How should I create this field, yet another
ForeignKey(User), or some else?
Just looking for some fresh point of view, as it seems that in such stupid case I’m stuck with the django model system.
I would probably add a third model to represent a specific wager someone has placed, as it is conceivable that more than two people could enter into a bet. It would look something like this:
Django will automatically generate
user.wager_setandbet.wager_setbased on the foreign keys. This allows you to easily iterate and display the wagers for a bet, as well as the wagers from each user. You can also add aunique_togetherconstraint onUserandBetin the Wager table so that each user can only make one wager.When the betting is all done, and a winner has been selected, you just set
bet.winner.In case you run into it, you might see a warning about
related_nameby havingBetpoint toWagerandWagerpoint toBet. To fix, just addrelated_name=wagerstoWager.bet.