N
N
Nposk2020-03-04 10:37:33
Django
Nposk, 2020-03-04 10:37:33

How to check Django field uniqueness and show on form?

How to display an error on the form, such a record already exists?
did so

class OidForm(forms.Form):
    oidDescription = forms.CharField(max_length=100)
    oidSlug = forms.CharField(max_length=50)
    oid = forms.CharField(max_length=100)

    oidDescription.widget.attrs.update({'class': 'form-control'})
    oidSlug.widget.attrs.update({'class': 'form-control'})
    oid.widget.attrs.update({'class': 'form-control'})

    def clean_oid(self):
        data = self.cleaned_data['oid']
        # print(self.cleaned_data)
        if Oid.objects.filter(oid=data).exists():
            raise ValidationError('Already exists')
        return self


Got an error
Don't mix *args and **kwargs in call to reverse()!

Where did I go wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WStanley, 2020-03-04
@WStanley

In the model, you need to make the field unique unique=True
Then the form will return the error "there is already such a record"
link to the document

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question