mercredi 17 février 2016

django checkbox is checked in the cleaned data

I am using python 2.7 & django 1.7.2.

I have a form where the user can submit a boolean field on the form as a checkbox that can usually be null or unchecked.

However, when a start date field is not empty, either the finish date field must not be empty or the checkbox should be checked.

I have tried to write this myself but failed in validating the checkbox field syntax despite searching SO & Google.

Here is my models code:

employment_history_start_date = models.DateField(null=True, blank=True)
employment_history_current_employment = models.BooleanField(default=False)
employment_history_finish_date = models.DateField(null=True, blank=True)

Here is my forms cleaned code:

def clean_employment_history_finish_date1(self):

    employment_history_current_employment = self.cleaned_data.get('employment_history_current_employment')
    employment_history_start_date = self.cleaned_data.get('employment_history_start_date')
    employment_history_finish_date = self.cleaned_data.get('employment_history_finish_date')

    if employment_history_start_date is not None:
        if employment_history_finish_date is None and employment_history_current_employment is None:
            raise forms.ValidationError(_("You must enter a valid To date or select the Current Employment check-box."))

    return employment_history_finish_date

I am uncertain how to write the correct syntax for employment_history_current_employment is None so that the code evaluates if the checkbox/boolean field is unchecked.

Thanks.




Aucun commentaire:

Enregistrer un commentaire