表单中的 Django 独特模型字段验证

Django unique model fields validation in form

我有一个包含一些独特字段的模型,我正在为其编写一个表单。我发现了一些对 [validate_unique][1] 方法的引用,当你调用它时应该检查字段的唯一性但是我的表单 .is_valid() 总是 returns True.

我的测试用例:

class ServerFormTest( TestCase ):
    def setUp( self ):
        self.server = Server.objects.create( host = "127.0.0.1", name = "localhost" )

    def test_unique_name(self):
        form = ServerForm({
            'name': 'localhost',
            'host': '127.0.0.1'
        })

        self.assertFalse( form.is_valid( ) )

和我的表格:

class ServerForm( forms.ModelForm ):
    class Meta:
        model = Server
        fields = ('name', 'host')

    def clean( self ):
        self.validate_unique()
        return self.cleaned_data

服务器型号:

class Server( models.Model ):
    host = models.GenericIPAddressField( blank = False, null = False, unique = True )
    name = models.CharField( blank = False, null = False, unique = True, max_length = 55 )

validate_unique 是一个 Model 方法。

运行 超类 clean 方法应该负责给定 ModelForm.

的模型唯一性检查
class MyModelForm(forms.ModelForm):    
    def clean(self):
        cleaned_data = super(MyModelForm, self).clean()
        # additional cleaning here
        return cleaned_data

django 文档上有一个 warning 专门关于覆盖 ModelForms 上的清理,它会自动执行几个模型验证步骤。

我通过将validate_unique()添加到save()

来解决它
class Server( models.Model ):
    host = models.GenericIPAddressField( blank = False, null = False, unique = True )
    name = models.CharField( blank = False, null = False, unique = True, max_length = 55 )

    def save(self, *args,**kwargs):
        self.validate_unique()
        super(Server,self).save(*args, **kwargs) 

对我有用。我不了解你。

实现字段唯一性的简单方法,只需在模型字段中使用“unique=True”。示例:

email = models.EmailField(verbose_name=_('Email'), max_length=255, unique=True)

phone = models.CharField(verbose_name=_('Phone Number'), max_length=14, unique=True)

但是如果你真的想通过形式来实现,或者情况需要,比如上面的代码部分,这里phonenumber是一个字符字段。这里很难实现唯一性,因为对于系统 0171-xxxxxxx 和 0171xxxxxxx 是不同的数字,但实际上它们是相同的。它可以通过表单的 clean_phone 方法轻松验证。这里的数字是在检查唯一性之前从字符串(字符字段)中解析出来的。

    def clean_phone(self):
        phone = self.cleaned_data.get("phone")
        # parse digits from the string
        digit_list = re.findall("\d+", phone)
        phone = ''.join(digit_list)
        
        if CustomUser.objects.filter(phone=phone).exists():
            raise forms.ValidationError("phone number is already exists")
        return phone