[Django]-Designing model structure for django

4๐Ÿ‘

โœ…

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:

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

Django will automatically generate user.wager_set and bet.wager_set based 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 a unique_together constraint on User and Bet in 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_name by having Bet point to Wager and Wager point to Bet. To fix, just add related_name=wagers to Wager.bet.

๐Ÿ‘คtghw

1๐Ÿ‘

What you need is a Many-to-Many relation with extra data (e.g. the amount on the wager, the time entered,โ€ฆ)

There is a chaper on this in the excellent Django docs on writing models.

Tyler has already outlined the proper schema for this.

๐Ÿ‘คBer

Leave a comment