[Django]-Django Admin validation

6👍

Define a custom form for your model admin, and override the specific field to use a RegexField.

class MyModel(models.Model):
    myfield = models.CharField(max_length=10)


class MyModelForm(forms.ModelForm):
    myfield = forms.RegexField(regex=r'\w+')


class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

admin.site.register(MyModel, MyModelAdmin)

0👍

You can also use clean method in ur form and validiate the fields using regex.That will form validiation.You can raise error on what u want

Leave a comment