Django独特的模型字段验证形式

时间:2015-07-01 00:58:12

标签: django django-forms

我有一个带有一些独特字段的模型,我正在为它编写表单。我找到了一些[validate_unique][1]方法的引用,该方法应该在您调用字段时检查字段的唯一性,但我的表单.is_valid()始终返回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 )

3 个答案:

答案 0 :(得分:11)

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专门用于覆盖ModelForm上的干净,它会自动执行多个模型验证步骤。

答案 1 :(得分:2)

我通过将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) 

它对我有用。我不了解你。

答案 2 :(得分:1)

一种实现字段唯一性的简单方法,只需在模型字段中使用“ 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)

但是,如果您真的想通过表单来实现此功能,或者在情况需要时,例如在上面的代码部分中,此处的电话号码是一个字符字段。这里很难实现唯一性,因为对于系统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