Link to home
Start Free TrialLog in
Avatar of tbener
tbener

asked on

Django multiple formsets of the same inline model

I have a model structure that lets the user define a page.

The code below shows a subset of it, and while I'm having no problem displaying the form with the formsets, I'm having trouble saving it, and then displaying it again.

Models.py

class Entity(models.Model):
    title       = models.CharField(max_length=50)

class ValueDefinition(models.Model):
    title       = models.CharField(max_length=50)   
    entity      = models.ForeignKey("Entity", null=True)
    order       = models.PositiveSmallIntegerField(default=0)

class Value(models.Model):
    value_definition    = models.ForeignKey("ValueDefinition")
    is_default          = models.BooleanField(default=False)  
    value_type          = models.CharField(max_length=50)
    char_value          = models.CharField(max_length=200, null=True, blank=True)
    integer_value       = models.IntegerField(null=True, blank=True)
    boolean_value       = models.BooleanField()

Open in new window


As you can see I have: Entity -> ValueDef -> Value. With Foreign Keys upstream. Obviously I took out many more fields and left in the fields that will let you get the idea.

Problem is: I want to display, as a form, a series of entities, and their related Values. Of course the whole page is much more complex, but here is the relevant piece of code of the View:

Views.py:

def my_view(request, page)

    # some more code here...

    ValueInlineFormSet = inlineformset_factory(ValueDefinition, Value, ValueForm, extra=0)

    for entity in page.entities:
        vdef = entity.valuedefinition_set.first()
        if vdef:
            prefix = 'value_set-%s' % entity.pk
            if request.method == 'POST':
                formset = ValueInlineFormSet(request.POST, instance=vdef, prefix = prefix)
                if formset.is_valid():
                    formset.save()
            else:
                formset = ValueInlineFormSet(instance=vdef, prefix = prefix)

            entity.values_formset = formset

    return render(request, 'myapp/page.html', {'page': page})

Open in new window


So now is the tricky part. Everything looks fine without the POST part. Meaning all the entities, where every entity has its Values inline formset - all are rendered properly. My problem is getting the relevant formset from the request.POST. When I call ValueInlineFormSet(request.POST, ...) I get back the full set of formsets, which includes all formsets of all entities! Then what happens is that the values are not saved, and obviously, the page doesn't look the same.

I intentionally didn't include the page template in order not to make things more complicated than they are (and I don't think it's really relevant), but let me know if you think it's relevant for the complete picture.

The HTML renders:

A form
Inside the form - loop over entities
inside an entity - loop the values formset
inside a form - display it's fields (django form, not html form!)
Thanks.
Avatar of Member_2_8038612
Member_2_8038612

This question has been posted a long time ago, but If you haven't found the solution or for the purpose of tracking such an issue, I will try to answer it here.

Since you haven't displayed the errors using
formset.errors

Open in new window

, my best bet would be that the formset haven't been validated in order to proceed further and save, even if there are errors that are not related to form, you can get using
formset.non_form_errors()

Open in new window


If you have the setup or saved previous logs, can you share it here ?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.