2👍
here is the working solution:
def clean(self):
cd_nvdf = super(NationalityVisaDetailsForm, self).clean()
if not any(
cd_nvdf.get(x, '')
for x in (
'nationality_visa_country_of_birth',
'nationality_visa_citizenship',
'nationality_visa_residency',
'nationality_visa_work_visa',
'nationality_visa_study_visa',
'nationality_visa_specialist_visa',
'nationality_visa_other_visa',
'nationality_visa_current_valid_passport_display_type',
'nationality_visa_passport_nationality',
)
):
self._errors['nationality_visa_country_of_birth'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_citizenship'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_residency'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_work_visa'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_study_visa'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_specialist_visa'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_other_visa'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_current_valid_passport_display_type'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
self._errors['nationality_visa_passport_nationality'] = self.error_class([_("You must enter at least one Nationality & Visa Detail.")])
return cd_nvdf
I hope that this will help someone.
0👍
There’s probably some Django details that will assist you, but I would recommend considering the Python first.
For the first task, I think you could probably benefit from a filter
.
First, we need a list of the values you want to check:
values_we_care_about = ['nationality_visa_country_of_birth'
'nationality_visa_citizenship'
'nationality_visa_residency'
'nationality_visa_work_visa',
etc.]
Next, we need to distill out the logic you’re using to check stuff into a simple function
def checking_stuff_function(value_cared_about):
return value_cared_about in cd_nvdf and cd_nvdf.get(value_cared_about, None)
Now, we can take this function and use it to filter all the values (if you’re on Python2, this will return a list right away, but if you’re on Python3, you’ll get a generator; I’m going to assume Python2):
checked_values = filter(checking_stuff_function, values_we_care_about)
Now, we can check how many checked_values
made it through. You can probably already see where this is going…
if len(checked_values) < 1:
# There's an error!
Now, you just need to reset your form (and because we went with a filter before, why not go with a map
here?):
map(lambda val: del self.cleaned_data[val], values_we_care_about]
I’ve never done that with a form, though. Is it really necessary to delete them on the overridden clean method? Can’t this be handled better in the view? I’ll leave that up to someone else to answer.
After that you can just return your form.
EDIT
I forgot about your errors. That can also be done in a manner similar to what we’ve done here and so I won’t try to spell it out.
Pretty much what you’re original code is missing is the DRY principle: ‘don’t repeat yourself.’ Try to find common patterns in what you’re doing and use those patterns to your advantage. If you’re writing the same array of strings in three or four places, that’s a problem. Save them to a list and iterate over them instead. Much less code to read.
- [Django]-What is the difference between django html_message and message in send mail
- [Django]-Using ViewerJS with Django
- [Django]-Set image size limit and resize image if is needed
- [Django]-Django key based session expiration