Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 是否可以在不覆盖表单字段的情况下从表单字段中消失所有验证器?_Django_Django Forms - Fatal编程技术网

Django 是否可以在不覆盖表单字段的情况下从表单字段中消失所有验证器?

Django 是否可以在不覆盖表单字段的情况下从表单字段中消失所有验证器?,django,django-forms,Django,Django Forms,我在Django 1.5.1中有以下环境: forms.py models.py 我修改了*auth_user*,允许我插入长度超过30个字符的用户名,这样我就可以使用电子邮件作为用户名(我已经尝试了OneToOneField和自定义模型,这些方法导致我的工作量超出预期,因此我不打算采用这种方法) 我的问题是: 由于auth.models.User没有按要求添加名字和姓氏清理器,并且widget对象不允许我设置所需的属性,因此我必须添加名字和姓氏清理器,这是最好的方法吗 我只有一个问题,用户名字

我在Django 1.5.1中有以下环境:

forms.py models.py 我修改了*auth_user*,允许我插入长度超过30个字符的用户名,这样我就可以使用电子邮件作为用户名(我已经尝试了OneToOneField和自定义模型,这些方法导致我的工作量超出预期,因此我不打算采用这种方法)

我的问题是:
  • 由于auth.models.User没有按要求添加名字和姓氏清理器,并且widget对象不允许我设置所需的属性,因此我必须添加名字和姓氏清理器,这是最好的方法吗
  • 我只有一个问题,用户名字段的MaxValueValidator设置最多为30。我怎样才能在不覆盖表单的情况下使其消失呢。因为我使用的是基于类的视图,所以当我试图通过UpdateView类更新表单时,重写字段不会用模型数据启动username字段,使其为空。其余字段没有任何问题

  • 如果我的英语看起来很难看,谢谢你,对不起

    好的,我发现了如何避免这种行为,因为MaxValueValidator在模型端运行,我在forms.py中重写了
    clean_username
    ,并添加
    self.\u meta.exclude+=('username',)
    ,以避免在模型中验证。当然,如果您的元类中没有任何
    exclude
    属性,那么只需使用
    self即可。_Meta.exclude=('username',)

    至于第1点,可以在类初始化时完成
    class UserForm(forms.ModelForm):
        class Meta:
            model = User
            widgets = {
                'username': forms.TextInput(attrs={'class': 'input-xlarge', 'placeholder': 'Email Address'}),
                'first_name': forms.TextInput(attrs={'class': 'input-xlarge', 'placeholder': 'First Name'}),
                'last_name': forms.TextInput(attrs={'class': 'input-xlarge', 'placeholder': 'Last Name'}),
                'mobile_phone': forms.TextInput(attrs={'class': 'input-xlarge', 'placeholder': 'Mobile Phone'}),
                'office_phone': forms.TextInput(attrs={'class': 'input-xlarge', 'placeholder': 'Office Phone'}),
            }
    
        GROUP_CHOICES = [(-1, '[Select]')]
        GROUP_CHOICES += [(group.id, group.name.capitalize()) for group in Group.objects.all()]
    
        username = forms.EmailField(
            label='Email Address',
            required=True,
            validators=[validate_email],
            widget=forms.TextInput(attrs={'class': 'input-xlarge', 'placeholder': 'Email Address'})
        )
        password1 = forms.CharField(
            label='Password',
            widget=forms.PasswordInput(attrs={'class': 'input-xlarge', 'placeholder': 'Password'})
        )
        password2 = forms.CharField(
            label='Password confirmation',
            widget=forms.PasswordInput(attrs={'class': 'input-xlarge', 'placeholder': 'Password confirmation'})
        )
        group = forms.ChoiceField(
            label='Group',
            choices=GROUP_CHOICES
        )
    
        def clean_password2(self):
            password1 = self.cleaned_data.get('password1')
            password2 = self.cleaned_data.get('password2')
            if password1 and password2 and password1 != password2:
                raise forms.ValidationError('Passwords don\'t match')
            return password2
    
        def clean_group(self):
            if self.cleaned_data['group'] == '-1':
                raise forms.ValidationError('This field is required.')
            return self.cleaned_data['group']
    
        def clean_first_name(self):
            if self.cleaned_data['first_name'] == '':
                raise forms.ValidationError('This field is required.')
            return self.cleaned_data['first_name']
    
        def clean_last_name(self):
            if self.cleaned_data['last_name'] == '':
                raise forms.ValidationError('This field is required.')
            return self.cleaned_data['last_name']
    
        def save(self, commit=True):
            user = super(UserForm, self).save(commit=False)
            user.set_password(self.cleaned_data['password1'])
            user.username = self.cleaned_data['username']
            if commit:
                user.save()
                user.groups.clear()
                user.groups.add(self.cleaned_data['group'])
                user.save()
            return user
    
    class User(auth.models.User):
        mobile_phone = models.CharField(
            verbose_name='Mobile Phone',
            max_length=50
        )
        office_phone = models.CharField(
            verbose_name='Office Phone',
            max_length=50
        )