0

Django 1.5.1 には次の環境があります。

フォーム.py

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

models.py

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
    )

* auth_user *を変更して、30 文字を超えるユーザー名を挿入できるようにして、ユーザー名として電子メールを使用できるようにします(すでにOneToOneFieldとカスタム モデルで試しましたが、これらのアプローチは予想よりも多くの作業を引き起こしたので、その方法を取るつもりはありません)

私の質問:

  1. first_name と last_name のクリーナーを追加する必要がありました。これは、auth.models.User に必要なクリーナーがなく、ウィジェット オブジェクトで必要な属性を設定できないためです。これが最善の方法ですか?
  2. 1 つだけ問題があります。ユーザー名フィールドのMaxValueValidator設定は最大でも 30 です。フォームでオーバーライドせずに、どうすればそれを消すことができますか。クラスベースのビューを使用しているため、フィールドをオーバーライドしても、UpdateViewクラスを介してフォームを更新しようとすると、モデル データでユーザー名フィールドが開始されず、空のままになります。残りのフィールドは問題ありません

私の英語が醜く見えたら、ありがとうございます。

4

1 に答える 1

0

MaxValueValidator がモデル側で実行されていたため、この動作を回避する方法を見つけました。forms.pyでオーバーライドし、モデルで検証されないようにclean_username追加しました。もちろん、 Meta クラスにプロパティself._meta.exclude += ('username',)がない場合は、単に使用しますexcludeself._meta.exclude = ('username',)

于 2013-04-22T10:47:05.063 に答える