views:

9

answers:

0

I am generating multple forms on one page using signals like:

def send_articleform(sender, form, request, type=None, nid=None, **kwargs):   
    if sender == 'article':
        if nid == None:    
            if request != None:
                formset = modelformset_factory(Article,  form=ArticleModelForm)
                form['article'] = formset(request.POST, request.FILES)

            else:
                formset = modelformset_factory(Article,  form=ArticleModelForm)
                form['article'] = formset(queryset=Article.objects.none())

        else:
            if request != None:
                formset = modelformset_factory(Article,  form=ArticleModelForm)
                form['article'] = formset(request.POST, request.FILES)
            else:
                formset = modelformset_factory(Article, max_num=1,  form=ArticleModelForm)
                form['article'] = formset(queryset=Article.objects.filter(pk=nid))        
signals.node_modelform.connect(send_articleform)

Regular formsets in a similar way:

def _send_media_attachform_(sender, form, request, type=None, nid=None, **kwargs):
    if nid == None:    
        if request != None:
            formset = formset_factory(MediaAttachForm)
            form['media_attach'] = formset(request.POST, request.FILES)

        else:
            formset = formset_factory(MediaAttachForm)
            form['media_attach'] = formset()  

    else:
        if request != None:
            formset = formset_factory(MediaAttachForm)
            form['media_attach'] = formset(request.POST, request.FILES)
        else:
            formset = formset_factory(MediaAttachForm)
            form['media_attach'] = formset()    
signals.node_modelform.connect(_send_media_attachform_)

These are displayed in the following manner:

    if request.method == 'POST': 
        form = {}
        node = {}
        signals.node_modelform.send(sender=type, form=form, request=request, nid=nid)
        for key, value in form.iteritems():
            if value.is_valid() and hasattr(value, 'save'): ##For modelformsets.
                value.save()
                node[key] = value.cleaned_data[0]
                try:
                    node[key]['nid'] = nid
                    node[key]['instance'] = value.save()[0]
                except:
                    node[key]['nid'] = nid
                    node[key]['instance'] = instance
            elif value.is_valid(): ##For regular forms
                node[key] = value.cleaned_data[0]

The problem is that the modelforms do not validate when editing, but they do when submitting. It turns out that when editing content, only one form at a time, or else none of them validates.