我在Django应用程序中实现了以下架构,但我是Django的ORM新手:
简而言之,DayMenu列出了多个MenuItem。 (MenuItem只是DayMenu和Meal之间的多对多关系。)每个用户从DayMenu中选择一个MenuItem。 (此选项表示为UserItemChoice。)
在我们的初稿models.py
(下面)中,MenuItem被定义为DayMenu模型上的多对多字段。
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Meal(models.Model):
# field options: diet
MEAT = "MEAT"
VEGETARIAN = "VEGET"
HALAAL = "HALAA"
DIET_CHOICES = (
(MEAT, "Meat"),
(VEGETARIAN, "Vegetarian"),
(HALAAL, "Halaal"),
)
# field options: type
FREE = "FREE"
PAID = "PAID"
SKIP = "SKIP"
TYPE_CHOICES = (
(FREE, "Free"),
(PAID, "Paid"),
(SKIP, "Skip"),
)
# fields
cost = models.IntegerField(default=10)
description = models.CharField(max_length=120)
diet = models.CharField(max_length=5, choices=DIET_CHOICES)
type = models.CharField(max_length=5, choices=TYPE_CHOICES)
class DayMenu(models.Model):
# fields
date = models.DateField()
locked = models.BooleanField(default=False)
item = models.ManyToManyField(Meal) # TODO: confirm (replaces MenuItem in schema)
# class UserItemChoice(models.Model):
#
# # fields
# user = models.CharField() # FIXME
# menuitem = models.CharField() # FIXME
# selected = models.BooleanField(default=False)
# like = models.NullBooleanField(default=None)
如何定义UserItemChoice:
答案 0 :(得分:2)
我认为您想要的是将UserItemChoice
定义为through
和User
之间m2m关系的MenuItem
模型。当您想要在m2m关系之间定义一些额外属性时,主要使用through
模型。
此处用户可能有多个MenuItem
,但您还需要与selected
和like
属性相关的属性,但将这2个属性移动到任一模型都是好的,因此through
是最好的解决方案。
查看关于through
definition and example的django doc。