Passing initial data to model formsets - a suggested improvement

193 views
Skip to first unread message

Mark Gensler

unread,
Sep 6, 2018, 7:53:05 AM9/6/18
to Django developers (Contributions to Django itself)
Initial data for modelformsets is treated in an odd way, which was outlined and discussed in the issues below


In summary, if the initial data provided to BaseModelFormSet.__init__ is longer than the number of extra forms, the excess data won't be rendered in forms.

This causes problems when
    a) writing abstract code which constructs the class separately from initialising it, or
    b) the code reuses the same class with differing initial data.

In these cases the length of the initial data may not be known at the time of class construction.

In order to minimise regressions I'd suggest that model formsets use max(self.extra, len(self.initial_extra)) in place of self.extra when calculating total_form_count().

I can't see this causing major regressions as if a user would like to limit the length of the forms they should already be doing so with the `max_num` parameter to the modelformset_factory. Othewrise, why not use the additional data in self.initial_extra?

Mark

Carlton Gibson

unread,
Sep 10, 2018, 5:59:36 AM9/10/18
to Django developers (Contributions to Django itself)
Hi Mark. 

What I'm not sure about here is how things have changed from Carl's original observation in the comment you link.

On Thursday, 6 September 2018 13:53:05 UTC+2, Mark Gensler wrote:

> ... if it's an improvement at all, it's not enough of one to justify making a backwards-incompatible change. 

As such I'm initially sceptical. 

I think I'm not really seeing your use-case, so perhaps if you motivated that some... 

What you do say is this: 

> ...the length of the initial data may not be known at the time of class construction. 

OK. But there's nothing to stop you defining a base FormSet class which has all the knowns and then using `modelformset_factory()` to pass in the known-at-runtime `extra` value that you need. 


Another way of saying "perhaps if you motivated that some..." is, Why isn't that good enough? 

Kind Regards,

Carlton


Reply all
Reply to author
Forward
0 new messages