Is this ManyToMany relation the good way to link my models in django? -
our application model looks this:
class event(models.model): title = models.charfield(max_length=100) nutrient = models.manytomanyfield(food) drink = models.manytomanyfield(drink) attendee = models.manytomanyfield(userprofile, related_name="partying_in") class food(models.model): name = models.charfield(max_length=100) quantity = models.integerfield() class drink(models.model): name = models.charfield(max_length=100) quantity = models.integerfield() class userprofile(models.model): user = models.onetoonefield(user)
we have 2 tables nutrient , drinks because in our template, have textfield can come in nutrient , drinks name auto-complete.
attendees can brings nutrient , drinks included in nutrient , drink fields contained in event can bring more that.
i want find best model this: thinking adding "through" relation attendee manytomanyfield details attendee bringing.
class event(models.model): attendee = models.manytomanyfield(userprofile, related_name="partying_in", through="bringing") class bringing(models.model): user = models.foreigngkey(userprofile) event = models.foreignkey(event) nutrient = models.manytomanyfield(food) drink = models.manytomanyfield(drink)
as django models, i'm not sure picked solution... if 1 of guys can help me or give me advices appreciate!
if model good, assume hard part create query when attendee wants bring...
straigh out of brain:
food1 = included in things bring event food2 = not included in eventhere query :
b1 = bringing(user=request.user, event=my_event, food=[food1,food2]) b1.save()
i assume not work food2 not in nutrient event ? how test if nutrient in list , if not, add together automatically minimum query possible? see lot of 'if' like:
for f in [food1,food2]: if f not in event.food.all(): something...
what think ?
django django-models
No comments:
Post a Comment