Django form validation making required conditional

0 votes

I'm new to Django (and Python), and am trying to figure out how to conditionalize certain aspects of form validation. In this case, there's a HTML interface to the application where the user can choose a date and a time from widgets. The clean method on the form object takes the values of the time and date fields and turns them back into a datetime.

In addition to the HTML interface, there's also an iPhone client making calls into the application, and I'd like to pass a UNIX timestamp-style time value in.

My form code looks like this:

class FooForm(forms.ModelForm):
    foo_date             = forms.CharField(required=True, widget=forms.RadioSelect(choices=DATE_CHOICES))
    foo_time             = forms.CharField(required=True, widget=SelectTimeWidget())
    foo_timestamp        = forms.CharField(required=False)

How do I make foo_date and foo_time required unless foo_timestamp is provided?

Aug 30, 2018 in Python by bug_seeker
• 15,550 points
3,554 views

1 answer to this question.

0 votes

This is done with the clean method on the form. You need to set foo_date and foo_time to required=False, though, because clean is only called after every field has been validated (see also the documentation).

class FooForm(forms.Form)
    # your field definitions

    def clean(self):
        data = self.cleaned_data
        if data.get('foo_timestamp', None) or (data.get('foo_date', None) and data.get('foo_time', None)):
            return data
        else:
            raise forms.ValidationError('Provide either a date and time or a timestamp')
answered Aug 30, 2018 by Priyaj
• 58,100 points

Related Questions In Python

0 votes
1 answer

Model Form “object has no attribute 'cleaned_data'” in Django?

Hello @kartik, For some reason, you're re-instantiating the ...READ MORE

answered Apr 29, 2020 in Python by Niroj
• 82,560 points
3,556 views
0 votes
1 answer

Errors in fields not displaying on Django admin form?

Hiii, Here is how you can see what ...READ MORE

answered Apr 29, 2020 in Python by Niroj
• 82,560 points
1,567 views
0 votes
1 answer

Django: Display a custom error message for admin validation error

Hello @kartik, Without looking, it sounds like the ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,560 points
1,831 views
+1 vote
1 answer

How can I build multiple submit buttons django form?

Hiii @kartik, You can use self.data in the clean_email method to access ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,560 points
525 views
0 votes
1 answer

how to download and install Django rest framework?

To install Django, you can simply open ...READ MORE

answered Apr 24, 2018 in Python by Christine
• 15,830 points
621 views
0 votes
1 answer
0 votes
1 answer

Host not allowed

Go to your project directory cd project cd project ALLOWED_HOSTS ...READ MORE

answered Aug 9, 2018 in AWS by Priyaj
• 58,100 points
746 views
+1 vote
1 answer

Why is openpyxl is required for loading excel format files?

Well, it sounds like openpyxl is not ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,100 points
270 views
0 votes
1 answer