1

I have a problem with my form validation in Django.

Let's say I have 2 apartments with option for booking them. User should choose start_date and end_date when he books apartment. When I'm validating my form for booking I check few conditions and one of them is to see if there are booked days between start and end date. If there is then the error is raised. It was working fine for one apartment in database. It looked like this:

class BookingForm(forms.ModelForm):

    class Meta:
        model = Booking
        fields = ['booking_start', 'booking_end', 'gsm']

    def clean(self):
        cleaned_data = self.cleaned_data
        form_booking_start = cleaned_data.get("booking_start")
        form_booking_end = cleaned_data.get("booking_end")               
        the_apartment = Apartman.objects.get(id=2)
        between = Booking.objects.filter(booking_start__gte=form_booking_start, booking_end__lte=form_booking_end).exists()


        if form_booking_start and form_booking_end:
            ...

            if between:
                raise forms.ValidationError("It seems like there are days that are already booked between your booking start and booking end. Please take care of it and book days that are not already booked.")

        return cleaned_data

But the problem is that when I add another apartment error is raised every time if user book dates which are already booked for some other apartment. So I need to check id of that apartment and then just check if the booking is related to that apartment. So if my apartment id=2 then it should be like this:

between = Booking.objects.filter(booking_start__gte=form_booking_start, booking_end__lte=form_booking_end, apartman__id=2).exists()

Problem is, I don't know how to pass value of that id from view to my form. My view looks something like this:

def booking(request, apartman_id):
    a = Apartman.objects.get(id=apartman_id)
    if request.method == 'POST':
            f = BookingForm(request.POST)
            if f.is_valid():
        ....
    else:
        f = BookingForm()

So, finally, how can I pass value of that apartman_id to my form?

ivan.zd
  • 674
  • 9
  • 13
  • This should give you an idea: http://stackoverflow.com/questions/9723465/django-form-passing-parameter-from-view-py-to-forms-gives-out-error – karthikr Sep 25 '13 at 14:44
  • I've already seen that post but it didn't really help me alot. I tried to mofidy my code to look like this FORMS.PY def __init__(self,*args,**kwargs): form_apartman_id=kwargs.pop("apartman_id") super(BookingForm, self).__init__(*args,**kwargs) VIEWS.PY f = BookingForm(apartman_id=form_apartman_id) But it gives me same errors as in that post global name 'form_apartman_id' is not defined I'm really not sure what to change to make it work. :/ – ivan.zd Sep 25 '13 at 16:26

1 Answers1

2

Your __init__() method should set the parameter on instance and use it in clean() method as

FORMS.PY

def __init__(self,*args,**kwargs): 
    #use self to store id
    self.form_apartman_id=kwargs.pop("apartman_id") 
    super(BookingForm, self).__init__(*args,**kwargs)

def clean(self):
   ....
   the_apartment = Apartman.objects.get(id=self.form_apartman_id)

Views.py

def booking(request, apartman_id):
    a = Apartman.objects.get(id=apartman_id)
    if request.method == 'POST':
            f = BookingForm(apartman_id=apartman_id, request.POST)
            if f.is_valid():
        ....
    else:
        f = BookingForm(apartman_id=apartman_id)
Rohan
  • 52,392
  • 12
  • 90
  • 87