Django remove form error for unique field

I have a Django model with a field customer_code

that is set to be unique, but only some users will have an assigned value for that field. Other users simply use this code to find the user who provided the code as a reference number. When they submit the form, however, it throws an error because the field has a unique value.

I want to remove this error after checking. The user does not receive a save with the set value None

before saving. I have tried to do it so far with a custom method clean()

in the form:

def clean(self):
    super(EmployeeForm, self).clean()
    if 'customer_code' in self.errors:
        del self._errors['customer_code']
    return self

      

But it doesn't work. Thank you all, thank you.

+3


source to share


1 answer


At the end of the method, you should return cleaned_data



 cleaned_data = super(EmployeeForm, self).clean()
 ...
 return cleaned_data

      

+1


source







All Articles