我在Django中有一个简单的模型,看起来像:
class TimeTable(models.Model):
title = models.CharField(max_length=100)
start_time= models.CharField(choices=MY_CHOICES, max_length=10)
end_time = models.CharField(choices=MY_CHOICES, max_length=10)
day1 = models.BooleanField(default=False)
day2 = models.BooleanField(default=False)
day3 = models.BooleanField(default=False)
day4 = models.BooleanField(default=False)
day5 = models.BooleanField(default=False)
day6 = models.BooleanField(default=False)
day7 = models.BooleanField(default=False)
对于此模型,我有2种形式:
class TimeTableForm(ModelForm):
class Meta:
model = TimeTable
fields = ['title ', 'start_time', 'end_time']
class WeekDayForm(ModelForm):
class Meta:
model = TimeTable
fields = ['day1', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7']
现在在views.py中,我需要将此值保存到数据库
def schedule(request):
if request.method == 'POST':
form = TimeTableForm(request.POST)
day_week = WeekDayForm(request.POST)
if all([form.is_valid(), day_week.is_valid()]):
form.save()
day_week.save()
我是Django的新手,所以我想将此值合并为一个,我将获得适当的数据,但是对于每个提交,我都会在数据库中获得两个独立的对象,例如
title | 8:00 | 10:00 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| | | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
但我应该得到
title | 8:00 | 10:00 | 0 | 1 | 1 | 1 | 1 | 0 | 0 |
如您所见,这两种形式的值在数据库中是分开的(很可能对于与django有更多共同之处的人来说很明显),可以选择将这两种形式组合成一个吗?