在编辑预订 django 时使编辑功能无冲突
Make edit funcion without conflict on edit booking django
我制作了具有过滤功能的巴士预订系统,以防止在同一日期重复预订同一辆巴士..
这是 forms.py 上的代码:
class BookingEditForm(forms.ModelForm):
class Meta:
model = Booking
fields = '__all__'
widgets = {
'idbooking': forms.HiddenInput(attrs={'class': 'form-control', 'id': 'id_booking', 'style':'text-transform: uppercase'}),
'BusId': forms.Select(attrs={'class': 'form-control', 'id': 'bus_id'}),
'start' : forms.DateInput(format=('%Y-%m-%d'), attrs={'type': 'date', 'class': 'form-control', 'required':'true'}),
'end_date' : forms.DateInput(format=('%Y-%m-%d'), attrs={'type': 'date', 'class': 'form-control', 'required':'true'}),
}
def clean(self):
start = self.cleaned_data['start']
end_date = self.cleaned_data['end_date']
BusId = self.cleaned_data['BusId']
res_all = Booking.objects.all().filter(BusId=BusId)
for item in res_all:
if start <= item.start and item.start <= end_date or start <= item.end_date and item.end_date <= end_date:
raise forms.ValidationError("Bus already reserved on same date!")
views.py :
@login_required(login_url='login')
def edit_book(request, pk):
booking = Booking.objects.get(idbooking=pk)
booking_forms = BookingEditForm(instance=booking)
customer_forms = CustomerForm(initial={
'phone': request.GET.get('phone'),
'name': request.GET.get('name')
})
if request.method == 'POST':
booking_forms = BookingEditForm(request.POST)
if booking_forms.is_valid():
idbooking = booking_forms.cleaned_data['idbooking']
BusId = booking_forms.cleaned_data['BusId']
start = booking_forms.cleaned_data['start']
end_date = booking_forms.cleaned_data['end_date']
Booking.objects.filter(idbooking=booking).update(
idbooking=idbooking,
BusId=BusId,
start=start,
end_date=end_date,
),
return redirect('booking-list')
else:
print(booking_forms.errors)
context = {
'cform': customer_forms,
'form': booking_forms,
}
return render(request, 'booking/create_book.html', context)
现在,我需要帮助 运行 编辑过滤功能,因为它适用于创建预订
但是当我尝试在编辑时实施时,它不起作用
我觉得 idbooking
上有事可做
尝试使用 instance
属性进行更新操作。如下更改清洁方法。它应该工作
def clean(self):
start = self.cleaned_data['start']
end_date = self.cleaned_data['end_date']
BusId = self.cleaned_data['BusId']
if hasattr(self,'instance'): # if this raises error try if self.instance: instead
res_all = Booking.objects.exclude(BusId=self.instance.BusId).filter(BusId=BusId)
else:
res_all = Booking.objects.all().filter(BusId=BusId)
for item in res_all:
if start <= item.start and item.start <= end_date or start <= item.end_date and item.end_date <= end_date:
raise forms.ValidationError("Bus already reserved on same date!")
我制作了具有过滤功能的巴士预订系统,以防止在同一日期重复预订同一辆巴士..
这是 forms.py 上的代码:
class BookingEditForm(forms.ModelForm):
class Meta:
model = Booking
fields = '__all__'
widgets = {
'idbooking': forms.HiddenInput(attrs={'class': 'form-control', 'id': 'id_booking', 'style':'text-transform: uppercase'}),
'BusId': forms.Select(attrs={'class': 'form-control', 'id': 'bus_id'}),
'start' : forms.DateInput(format=('%Y-%m-%d'), attrs={'type': 'date', 'class': 'form-control', 'required':'true'}),
'end_date' : forms.DateInput(format=('%Y-%m-%d'), attrs={'type': 'date', 'class': 'form-control', 'required':'true'}),
}
def clean(self):
start = self.cleaned_data['start']
end_date = self.cleaned_data['end_date']
BusId = self.cleaned_data['BusId']
res_all = Booking.objects.all().filter(BusId=BusId)
for item in res_all:
if start <= item.start and item.start <= end_date or start <= item.end_date and item.end_date <= end_date:
raise forms.ValidationError("Bus already reserved on same date!")
views.py :
@login_required(login_url='login')
def edit_book(request, pk):
booking = Booking.objects.get(idbooking=pk)
booking_forms = BookingEditForm(instance=booking)
customer_forms = CustomerForm(initial={
'phone': request.GET.get('phone'),
'name': request.GET.get('name')
})
if request.method == 'POST':
booking_forms = BookingEditForm(request.POST)
if booking_forms.is_valid():
idbooking = booking_forms.cleaned_data['idbooking']
BusId = booking_forms.cleaned_data['BusId']
start = booking_forms.cleaned_data['start']
end_date = booking_forms.cleaned_data['end_date']
Booking.objects.filter(idbooking=booking).update(
idbooking=idbooking,
BusId=BusId,
start=start,
end_date=end_date,
),
return redirect('booking-list')
else:
print(booking_forms.errors)
context = {
'cform': customer_forms,
'form': booking_forms,
}
return render(request, 'booking/create_book.html', context)
现在,我需要帮助 运行 编辑过滤功能,因为它适用于创建预订 但是当我尝试在编辑时实施时,它不起作用
我觉得 idbooking
尝试使用 instance
属性进行更新操作。如下更改清洁方法。它应该工作
def clean(self):
start = self.cleaned_data['start']
end_date = self.cleaned_data['end_date']
BusId = self.cleaned_data['BusId']
if hasattr(self,'instance'): # if this raises error try if self.instance: instead
res_all = Booking.objects.exclude(BusId=self.instance.BusId).filter(BusId=BusId)
else:
res_all = Booking.objects.all().filter(BusId=BusId)
for item in res_all:
if start <= item.start and item.start <= end_date or start <= item.end_date and item.end_date <= end_date:
raise forms.ValidationError("Bus already reserved on same date!")