我试图同时在两个模型中进行更改。因此,根据论坛上的一个主题,我在一个视图中创建了两种形式并将其添加到一个html中。
我认为我的第二种形式做错了。为什么我在模型中的值不会更改为False?
看起来差不多。
views.pl
if request.method == 'POST' and 'btn_massage_order' in request.POST:
ordering_form = OrderingMassageForm(data=request.POST)
if ordering_form.is_valid():
ordering = ordering_form.save(commit=False)
massage_product = query_product # nazwa produkty
masseurs = query_user # massage
massage_time_interval = time # time example 60 min
price_massage = price # price
day_week = clean_my_date # day week
time_compartment = ordering_form.cleaned_data['time']
[...]
ordering.massage_product = massage_product
ordering.masseurs = masseurs
ordering.massage_time_interval = massage_time_interval
ordering.time_compartment = time_compartment
ordering.price = price_massage
ordering.day_week = day_week
[...]
ordering.save()
else:
ordering_form = OrderingMassageForm()
#next form i views
if request.method == 'POST' and 'btn_massage_order' in request.POST:
ordering_form = OrderingMassageForm(data=request.POST)
ordering_form_on_off = TimeOnTimeOff(data=request.POST)
if ordering_form_on_off.is_valid() and ordering_form.is_valid():
ordering_form_on_off = ordering_form_on_off.save(commit=False)
# the value that will be save
reservation = False
# I receive my object
time_compartment = ordering_form.cleaned_data['time']
# assigning my object
ordering_form_on_off.time_compartment = reservation
#save
ordering_form_on_off.save()
else:
ordering_form_on_off = TimeOnTimeOff()
forms.py
class OrderingMassageForm(forms.ModelForm):
class Meta:
model = OrderingMassage
fields = ('time',
'place',
'payment_method',
'name',
'surname',
[...]
class TimeOnTimeOff(forms.ModelForm):
class Meta:
model = Time
fields = ('free_or_no',
)
widgets = {
'free_or_no': forms.HiddenInput(),
}
models.py (在这种情况下,我尝试通过不起作用的第二种形式更改值)
class Time(models.Model):
day_time = models.ForeignKey(DayTime, on_delete=models.CASCADE)
compartment = models.CharField(max_length=11)
free_or_no = models.BooleanField(default=True)
time_equivalent = models.IntegerField()
模板
<form action="." method="post">
{% csrf_token %}
{{ ordering_form.as_p }}
<button type="submit" name="btn_massage_order" class="btn btn-primary">Potwierdż rezerwacje</button>
</form>
任何帮助将不胜感激。