I have a rental apartment booking page and the user has to state their arrival and departure dates in a booking form. I am trying to set up a date validator function that doesn’t allow the picked departure date to be before the arrival date. For this I need to get the value of the entered arrival_date field to use in the departure_before_arrival( ) function. Could anybody help out?
models.py
def departure_before_arrival(value):
if value < ??arrival_date??:
raise forms.ValidationError("Your departure date must be after your arrival date!")
return value
class Booking(models.Model):
[...]
arrival_date = models.DateField()
departure_date = models.DateField(validators=[departure_before_arrival])
[...]
I have been trying the suggested methods from this post but none work for me.
I have also tried
def get_value(self, obj):
return getattr(obj, self.name)
def departure_before_arrival(value):
if value < get_value(Booking, self.arrival_date):
raise forms.ValidationError("Your departure date must be after your arrival date!")
return value
but keep getting a NameError “name ‘arrival_date’ is not defined”