'Validation for unique in form

I have model and form like this

class Article(models.Model):
    key = models.CharField(max_length=20,null=False,unique=True)

class ArticleForm(forms.ModelForm):
    class Meta:
       model = Article
    key = forms.CharField(required=True)

This key is unique in table, so I want to validate in form too.

If I put the existing key, it comes to the

1062, "Duplicate entry 'inquiry' for key error in yellow screen

However it also doesn't work,

forms.CharField(required=True,unique=True)

How can I make this validation?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source