简体   繁体   English

Django Admin通用堆叠式内联表单集验证,用于空白表单

[英]Django Admin Generic Stacked Inline formset validation for empty form

I had a problem with Django Generic Stacked Inline Formset. 我在Django Generic Stacked Inline Formset上遇到问题。 I know it creates a formset for inlines , i have a generic model here which has been created as an inline which is then used further. 我知道它为inline创建了一个formset,我在这里有一个通用模型,该通用模型已经被创建为inline,然后被进一步使用。 My problem is that the first form of the formset is kept empty due to django library code https://github.com/django/django/blob/master/django/forms/formsets.py#L164 我的问题是由于django库代码https://github.com/django/django/blob/master/django/forms/formsets.py#L164,表单集的第一种形式保持为空

Ok , if i go and change the value of this line to False , then my formsets no longer are permitted to be empty. 好的,如果我去将这行的值更改为False,那么我的表单集将不再被允许为空。

I just wanted to check if the first form in my formset is blank and if it is then return back. 我只想检查表单集中的第一个表单是否为空白,然后返回。 Here's my code : admin.py 这是我的代码:admin.py

   class RequiredInlineFormSet(BaseFormSet):
        def __init__(self,*args,**kwargs):
            super(RequiredInlineFormSet,self).__init__(*args,**kwargs)
            for form in self.forms:
                form.empty_permitted=False

    class ContactInline(generic.GenericStackedInline):
        model=Contact
        verbose_name='Contact Details '
        fieldsets=(
        ('Basic Information',{
           'classes': ('grp-collapse grp-open',),
           'fields':('salutation','first_name','last_name','organization','office_phone','mobile_phone','home_phone','date_of_birth','email',
                     'secondary_email','email_opt_out','reference','do_not_call','notify_owner'
                     ),        
        }),
        ('Other',{
           'classes': ('grp-collapse grp-open',),
           'fields':('notes','profile_picture','related_contact'),        
        }),                      
        )
        max_num=1
        formset = RequiredInlineFormSet()

    class LeadAdmin(admin.ModelAdmin):
        inlines=(ContactInline,)
        save_on_top = True  

Sadly , this doesnt work , i am totally wrong somewhere i know. 可悲的是,这行不通,我在某个地方完全错了。 I just needed help so that i could not leave the first form in the formset blank and return an error if done so. 我只需要帮助,以使我无法将表单集中的第一个表单留空,如果这样做,则返回错误。 I tried searching SOF a lot , but really couldnt find something that could help me. 我尝试了很多SOF搜索,但确实找不到可以帮助我的东西。 Any help would be greatly appreaciated. 任何帮助将不胜感激。 Thanks a lot in advance. 非常感谢。

I was able to get it done , but through Javascript. 我能够通过Javascript完成它。 As mentioned my requirement was the validation of the form in the formset , which i was able to do through javascript. 如前所述,我的要求是验证formset中的表单,我能够通过javascript进行验证。 Hence it works. 因此,它起作用。

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

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