views:

165

answers:

2

Hi,

I'd like to validate user input with regular expression in Django Admin CharField... How is it possible?

Thanks in advance, Etam.

+2  A: 

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)
Daniel Roseman
+1  A: 

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

ha22109