design - Many to Many relationships in Django -


i working on design models in django , wanted advice. have model teams, of many users can part of. users can members of many teams, cannot members of same team twice. there separate information want track each team/user combo. on top of that, there user "admin" each team, each team may have single admin. have condensed data model definitions follows:

class team(models.model):     name = models.charfield()     members = models.manytomanyfield(user, through='membership')     admin = models.foreignkey(user)  class membership(models.model):     user = models.foreignkey(user)     team = models.foreignkey(team)     extra_data = models.charfield() 

so guess 2 questions are:

1) how make on membership model, no user , team combination appeared more once?

2) there better way signify admin on team, while making sure each team had single one? seems if i'm storing admin this, while enforcing rule of having single admin, becomes cumbersome query team members since admin not in membership table. , if stored in membership, i'd have perform query see if admin of team or not. thought of using "is_admin" field in membership popped in head, i'm unsure of how keep constraints of 1 admin per team.

any greaty appreciated.

update: looks unique_together meta tag i'm looking on first question. i'm still curious second 1 though....

first question:

you can add constraint prevent it:

class membership(models.model):     user = models.foreignkey(user)     team = models.foreignkey(team)     extra_data = models.charfield()     class meta:         unique_together = (('user','team'),) 

second question(s):

you add ranking field membership model , make 'user' , 'rank' unique_together, positive integer field. , user rank == 1 admin. or similar. enforcing scheme, can cumbersome code maintenance of it. may better off you've got.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -