简体   繁体   中英

Django Admin Generic Stacked Inline formset validation for empty form

I had a problem with 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. 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

Ok , if i go and change the value of this line to False , then my formsets no longer are permitted to be empty.

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

   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. Any help would be greatly appreaciated. Thanks a lot in advance.

I was able to get it done , but through Javascript. As mentioned my requirement was the validation of the form in the formset , which i was able to do through javascript. Hence it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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