[Answered ]-Django form validation with parameters from view

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

Leave a comment