python - Django ternary relationships -
i quite new django , having troubles creating right models particular problem.
i have users, , own "wallet". wallet should list of currencies/values.
currencies in table "name" field. id : 1 name : eur
and i'd wallet : currency : eur, value : 0, currency : usd, value : 1000
for this, i'd use ternary table let's balance. balance have : wallet id : 1, currency id : 1, value : 50
i want right thing here, , not sure how proceed.
thanks
class wallet(models.model): # if user has 1 wallet user = models.onetoonefield('user') # if user has multiple wallets # user = models.foreignkey('user') class balance(models.model): currency = models.charfield(max_length=10) value = models.decimalfield() wallet = models.foreignkey('wallet') # user's wallet: user_wallet = user.wallet # balances user_balance = user.wallet.balance_set.all()
edit:
currency
doesn't have model on own, because there aren't many currencies in there. can avoid joining additional tables when query. use choices
on charfield
instead:
currencies = ( (u'usd', u'us dollars'), (u'eur', u'euro'), # ... ) currency = models.charfield(max_length=3, choices=currencies)
but again, if have lot of currencies, go model.
if using oneonone
relationship between wallet
, user
, doesn't matter it. if have multiple wallets 1 user, think should figure out why user
in wallet
, right?
Comments
Post a Comment