简体   繁体   中英

Django Admin validation

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

Thanks in advance, Etam.

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)

您也可以在ur表单中使用clean方法并使用正则表达式验证字段。这将形成验证。您可以提出您想要的错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM