【问题标题】:Adding modelForm validation constraints in Django for a Meta class在 Django 中为 Meta 类添加 modelForm 验证约束
【发布时间】:2020-08-05 13:39:12
【问题描述】:

我需要在我的CustomUserCreationForm Meta class 上添加一些验证约束。

我找不到添加它的位置和方法。我是否应该为此使用 RegexValidator?

例如,我希望 first_name 的最小长度为 2,最大长度仅为 30 个字母。


from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.forms import ModelForm

from .models import *

import datetime


class CustomUserCreationForm(UserCreationForm):
    birth_date = forms.DateField(
        label='birthdate', 
        widget=forms.DateInput(
            attrs={'placeholder':'jj/mm/aaaa'}
        )
    )
    introduction = forms.CharField(
        label='introduction', 
        min_length=20, 
        max_length=500,
        widget = forms.Textarea(
            attrs={
                'placeholder': 'Hey, I\'m Wely.',
                'cols':"40", 
                'rows':"5"
            }
        )
    )
    address1 = forms.CharField(
        label='Address (1st line)', 
        min_length=2, 
        max_length=40,
        widget = forms.TextInput(
            attrs={'placeholder': '1st av. Lincoln'}
            )
        )
    address2 = forms.CharField(
        required=False, 
        label='Address (2nd line)', 
        min_length=0, 
        max_length=40,
        widget = forms.TextInput(
            attrs={'placeholder': 'Apt 3'}
        )
    )
    registration_video = forms.CharField(
        label='video',
        widget = forms.FileInput(
            attrs={'style': 'display:none'}
        )
    )


    class Meta:
        model = CustomUser
        fields = ('first_name', 'last_name', 'email')
        labels = {
            'first_name': 'First name',
            'last_name': 'Last Name',
            'email': 'E-mail address'
        }
        widgets = {
            'first_name': forms.TextInput(attrs = {'placeholder': 'Alicia'}),
            'last_name': forms.TextInput(attrs = {'placeholder': 'Rodriguez'}),
            'email': forms.EmailInput(attrs = {'placeholder': 'alicia@mail.com'}),
        }


【问题讨论】:

    标签: django-forms django-widget


    【解决方案1】:

    不使用 RegexValidator。只能这样改。这里我们在 attr 中为 first_name 字段添加 minlengthmaxlength

    class Meta:
            model = CustomUser
            fields = ('first_name', 'last_name', 'email')
            labels = {
                'first_name': 'First name',
                'last_name': 'Last Name',
                'email': 'E-mail address'
            }
            widgets = {
                'first_name': forms.TextInput(attrs = {'placeholder': 'Alicia','minlength':2,'maxlength':30}),
                'last_name': forms.TextInput(attrs = {'placeholder': 'Rodriguez'}),
                'email': forms.EmailInput(attrs = {'placeholder': 'alicia@mail.com'}),
            }
    

    【讨论】:

    • 我以为我回答了。谢谢您的回答。不幸的是,我正在寻找一种不触及属性的方法。我认为至少这是可能的。
    猜你喜欢
    • 2018-05-30
    • 2015-11-22
    • 2019-03-11
    • 2017-04-26
    • 2012-09-30
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多