apardes
apardes

Reputation: 4380

Django remove form error for unique field

I have a Django model with a customer_code field that is set to unique but only some users will have an assigned value for this field. Other users simply use this code to find the user who provided their code as a reference number. When they submit the form however it raises an error as the field is set to unique.

I would like to remove this error upon validation. The user does not get saved with the value it is set to None before save. I have tried doing this so far with a custom clean() method on the form:

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

But this has not been working. All help is appreciated, thanks.

Upvotes: 3

Views: 3363

Answers (1)

madzohan
madzohan

Reputation: 11808

In the end of method you should return cleaned_data

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

Upvotes: 1

Related Questions