I have a product model, and a price model. The price model has a FK to the product model. There may be multiple prices (depending on the quantity of units sold). Previous to django 1.2.5 http://yergler.net/blog/2009/09/27/nested-formsets-with-django/ would sove the problem. But as detailed http://www.mail-archive.com/django-users@googlegroups.com/msg124195.html this no longer works.
End desired result: the following form to edit and add data.
Product One - Price One, Quantity - Price Two, Quantity Product Two - Price One, Quantity Product Three - Price One, Quantity - Price Two, Quantity - Price Three, Quantity
Reproduced for clarity:
In Nathan's original code, the lowest level formset is created like this:
TenantFormset(data=self.data,
instance=instance,
prefix='TENANTS_%s' % pk_value)
where instance is an instance of Building, the "parent" or container for tenants and self is an instance of
class BaseBuildingFormset(BaseInlineFormSet)
which is instantiated like this:
BuildingFormset = inlineformset_factory(models.Block,
models.Building,
formset=BaseBuildingFormset,
extra=1)
To get around this I stopped passing in self.data, which led to forms in the lowest layer always have is_bound = False, even after being rendered and POSTed back to the view. Consequently validation always fails and the objects that the forms represent cannot be updated. I've reproduced the behaviour using the code from Nathan's blog post, so it seems that this approach to nesting formsets is no longer valid, or the code needs a tweak to work at 1.3.
Up to Django 1.2.5, Nathan's code works fine. However at 1.3, if self.data
is passed to TenantFormset, it turns out to be empty and a ValidationError
is raised because the ManagementForm information has not been supplied.
This is the intended consequence of #11418, AFAICT.
Does anyone have any other ideas on how this might be made to work?
Cheers,
(Sorry about the absence of detailed code examples - Nathan's post is much clearer than my code, so I'd recommend you look at that.)
General Note: there seems much confusion about just how to do this, shown by responses here http://www.reddit.com/r/django/comments/hwyto/is_there_a_way_to_do_nested_formsets_in_django/