【发布时间】:2010-09-06 15:24:52
【问题描述】:
我想在保存 ModelForm 时将 BooleanField inuse 设置为 True(我正在使用管理区域之外的表单),但我不确定该怎么做。
型号:
class Location(models.Model):
place = models.CharField(max_length=100)
inuse = models.BooleanField()
class Booking(models.Model):
name = models.CharField(max_length=100, verbose_name="Your name*:")
place = models.ManyToManyField(Location, blank=True, null=True)
表格:
class BookingForm(ModelForm):
class Meta:
model = Booking
def save(self, commit=True):
booking = super(BookingForm, self).save(commit=False)
if commit:
booking.save()
self.save_m2m()
for location in booking.place.all():
location.inuse = True
print location #nothing prints
location.save()
查看:
def booking(request):
form = BookingForm()
if request.method == 'POST':
form = BookingForm(request.POST)
if form.is_valid():
form.save()
else:
form = form
return render_to_response('bookingform.html', {
'form': form,
})
已更新至最新版本(请参阅 Manoj Govindan's answer)。它仍然没有在提交/保存时将 inuse 更新为 True。
【问题讨论】:
-
还有其他型号吗?
Booking表单的底层模型是什么?Location和其他型号有什么关系? -
除了你看到的减去一些元等之外没有任何关系
-
models.BooleanField(default=True)有什么问题? -
这些位置是由管理员使用 False 添加的,我只希望当用户在预订表单上选择它们时将它们更新为 True。
标签: python django django-models django-forms