简体   繁体   English

始终会触发对Django中CreateView和UpdateView的验证,即使模型未/不应

[英]Validation on CreateView and UpdateView in django always being triggered, even when model doesn't/shouldn't

I am not sure why adding widgets in my GroupForm form in forms.py caused my validations to go haywire. 我不确定为什么在Forms.py的GroupForm表单中添加小部件会导致验证失败。 Before that they were respecting my models, now after adding widget attrs for everything it no longer respects the models and says a field is required for everything. 在此之前,他们尊重我的模型,现在为所有内容添加了小部件属性之后,它不再尊重模型,并说所有内容都需要一个字段。 Is there some other item I missed when defining the widget? 定义窗口小部件时,我是否还错过了其他项目?

forms.py: forms.py:

class GroupForm(forms.ModelForm):
    group_name = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'1', 'placeholder':'Groups name'}))
    group_contact = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'2', 'placeholder':'Groups point of contact person'}))
    tin = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'3', 'placeholder':'Groups tin#'}))
    npi = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'4', 'placeholder':'Groups npi#'}))
    notes = forms.CharField(widget = forms.Textarea(attrs={'tabindex':'5', 'placeholder':'Group notes'}))
    #notes = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'5', 'placeholder':'Groups notes'}))

    billing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'6'}))
    mailing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'7'}))
    start_date = forms.DateField(widget=forms.TextInput(attrs=
                                {
                                    'class':'datepicker',
                                    'tabindex' : '8',
                                    'placeholder' : 'Groups start date'
                                }))
    end_date = forms.DateField(widget=forms.TextInput(attrs=
                                {
                                    'class':'datepicker',
                                    'tabindex' : '9',
                                    'placeholder' : 'Groups term date'
                                }))
    change_date = forms.DateField(widget=forms.TextInput(attrs=
                                {
                                    'class':'datepicker',
                                    'tabindex' : '10',
                                    'placeholder' : 'Groups changed date'
                                }))

    change_text = forms.CharField(widget = forms.TextInput(attrs={'tabindex':'11', 'placeholder':'Reason for date change'}))
    #term_comment = forms.CharField(widget= forms.TextInput(attrs={'tabindex':'12', 'placeholder':'Note on group term'}))
    term_comment = forms.CharField(widget = forms.Textarea(attrs={'tabindex':'12',  'placeholder':'Note on group term'}))
    group_phone = forms.RegexField(regex=r'^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$', 
                                error_message = ("Phone number must be entered in the format: '555-555-5555 or 5555555555'. Up to 15 digits allowed."),
                                widget = forms.TextInput(attrs={'tabindex':'13', 'placeholder': '555-555-5555 or 5555555555'}))

    group_fax = forms.RegexField(regex=r'^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$', 
                                error_message = ("Fax number must be entered in the format: '555-555-5555 or 5555555555'. Up to 15 digits allowed."),
                                widget = forms.TextInput(attrs={'tabindex':'15', 'placeholder': '555-555-5555 or 5555555555'}))

    group_term = forms.ModelChoiceField(queryset=GroupTerm.objects.all(), widget=forms.Select(attrs={'tabindex':'16'}))

    class Meta:
        model=Group
        exclude = ['created_at', 'updated_at']

views.py: views.py:

class GroupCreateView(CreateView):
    model = Group
    form_class = GroupForm
    template_name = 'ipaswdb/group/group_form.html'
    success_url = 'ipaswdb/group/'

    def form_valid(self, form):
        return super(GroupCreateView, self).form_valid(form)

class GroupUpdateView(UpdateView):
    model = Group
    form_class = GroupForm
    template_name = 'ipaswdb/group/group_form.html'
    success_url = 'ipaswdb/group/'

Group model: 组模型:

class Group(models.Model):
    group_name = models.CharField(max_length=50)
    group_contact= models.CharField(max_length=50)
    tin = models.CharField(max_length=50)
    npi =models.CharField(max_length=50)
    notes = models.TextField(max_length = 255,  null=True, blank=True)
    billing_address = models.ForeignKey('Address', related_name = 'billing_address', on_delete=models.SET_NULL, null=True)
    mailing_address = models.ForeignKey('Address', related_name = 'mailing_address', on_delete=models.SET_NULL,  null=True, blank=True)
    start_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
    end_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
    change_date = models.DateField(auto_now=False, auto_now_add=False, null=True, blank=True)
    change_text = models.TextField(max_length = 255,  null=True, blank=True)
    term_comment = models.TextField(max_length = 255,  null=True, blank=True)
    group_phone=models.CharField(max_length=50)
    group_fax = models.CharField(max_length=50)
    group_term = models.ForeignKey(GroupTerm, on_delete=models.SET_NULL, null=True, blank=True) #quesiton is can a group be termed many times?
    created_at=models.DateField(auto_now_add=True)
    updated_at=models.DateField(auto_now=True)

    #provider_location = models.ManyToManyField('ProviderLocations', through='GroupLocations')

    def __str__(self):
        return self.group_name

It's not because you added the widgets, it's because you actually redefined the fields and while redefining them, you did not respect your model's requirements. 这并不是因为您添加了小部件,而是因为您实际上重新定义了字段,并且在重新定义它们时,您没有遵守模型的要求。 For example in your model 例如在您的模型中

 mailing_address = models.ForeignKey(..., null=True, blank=True)

mailing address is allowed to be empty, but in your defined form field it's required. 邮寄地址可以为空,但是在您定义的表单字段中是必填字段。

mailing_address = forms.ModelChoiceField(queryset=Address.objects.all(), widget=forms.Select(attrs={'tabindex':'7'}))
# You need required=False

If you want to redefine your own fields for the modelForm you can, then you need to respect your model while doing it. 如果要为modelForm重新定义自己的字段,则在执行模型时需要尊重模型。 However , you can also accomplish what you're trying by using already existing dictionaries in modelForm . 但是 ,您也可以通过使用modelForm已经存在的字典来完成您要尝试的操作。 For example inside your class Meta you can override the widgets like this: 例如,在您的class Meta您可以像这样覆盖小部件:

 class YourForm(ModelForm):
     class Meta:
        model = YourModel
        fields = ('field_1', 'field_2', 'field_3', ...)
        widgets = {
            # CHANGE THE WIDGETS HERE IF YOU WANT TO
            'field_1': Textarea(attrs={'cols': 80, 'rows': 20}),
        }  
        labels ={
            # CHANGE THE LABELS HERE IF YOU WANT TO
        }

More info at Django's modelForm docs: Overriding defaults field 有关Django的modelForm文档的更多信息: 覆盖默认值字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM