class StaffInfoAdmin(admin.ModelAdmin): inlines = [EmergencyInline, BackgroundInline, WorkInline, FamilyInline] search_fields = ['NAME', 'RES_ID'] list_display = ['NAME', 'BIRTH', 'RES_ID'] list_filter = ['POST'] def clean_fields(self, request, obj): mobile_pattern = re.compile(r'^1[3456789]\d{9}$') if not re.match(mobile_pattern, obj.TEL): raise ValidationError("请输入11位电话号码,且不包含'-'") super().clean_fields(request, obj)代码如上,但是没有完成合法性检查的功能,电话号码输入123依旧可以提交数据
时间: 2024-02-06 10:03:00 浏览: 122
根据你提供的代码,可以看出在`clean_fields`函数中进行了电话号码的合法性检查,但是这个函数并没有被调用。为了实现这个功能,你需要在`save_model`函数中手动调用`clean_fields`函数。修改后的代码如下:
```
class StaffInfoAdmin(admin.ModelAdmin):
inlines = [EmergencyInline, BackgroundInline, WorkInline, FamilyInline]
search_fields = ['NAME', 'RES_ID']
list_display = ['NAME', 'BIRTH', 'RES_ID']
list_filter = ['POST']
def clean_fields(self, request, obj):
mobile_pattern = re.compile(r'^1[3456789]\d{9}$')
if not re.match(mobile_pattern, obj.TEL):
raise ValidationError("请输入11位电话号码,且不包含'-'")
super().clean_fields(request, obj)
def save_model(self, request, obj, form, change):
self.clean_fields(request, obj)
super().save_model(request, obj, form, change)
```
这样就可以在保存数据之前进行合法性检查了。
阅读全文