models类
class IpInfo(models.Model):
ip = models.CharField(max_length = 20,unique=True)
network_type = models.CharField(max_length = 10,choices=(("PPPOE","pppoe"),("DHCP","dhcp"),("STATIC","static")))
modelfrom类:
class IpInfoForm(ModelForm):
class Meta:
model = models.IpInfo
fields = "__all__"
def clean(self):
ip_complie = re.compile(r'(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])')
ip = self.cleaned_data.get("ip")
flag = re.match(ip_complie, ip)
network_type = self.cleaned_data.get("network_type")
if network_type == "STATIC" and not(flag):
raise forms.ValidationError(message="IP格式不正确,请从新输入")
当添加clean()方法后,原IpInfoForm中“ip”字段中的unique就不会生效了,当前端提交一个重复的ip字段过来,后端直接报错
如果注释掉clean,则会正常验证,各位同僚看看问题出在哪里?我的需求是,既要验证的ip字段的unique,又要保留clean中的方法。