Set formfield.initial when created using modelformset_factory

70 views
Skip to first unread message

Brock Hallenbeck

unread,
May 23, 2018, 2:04:43 PM5/23/18
to Django developers (Contributions to Django itself)
When creating a formset using modelformset_factory and passing in a list of dictionaries to use as initial values, those values can be retrieved from the relevant form, and the relevant boundfield on that form.
However the formfield class itself has no knowledge of that initial data.

Is this intended behavior? I feel like I should be able to catch that initial value in my formfield subclasses.

Here is a dpaste illustrating what I am talking about:

Collin Anderson

unread,
May 23, 2018, 2:38:40 PM5/23/18
to django-d...@googlegroups.com
Hi Brock,

Yes, that's the intended behavior, and you can see the same behavior on a regular form, without using formsets, which might make it a little more clear what's going on:

class MyForm(forms.Form):
    name = forms.CharField()

form = MyForm(initial={'name': 'test'})
form.initial  #yields {'name':'test'}
form['name'].initial #yields 'test'
form.fields['name'].initial #yields None

The field itself is shared between all instances of MyForm, so passing in initial data to one instance of MyForm shouldn't modify the field. Instead, the BoundField is used to hold the data for a particular form, and as you mention, as the initial value on the boundfield has that value you expect.

Hope this helps,
Collin
 

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscribe@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brock Hallenbeck

unread,
May 23, 2018, 2:58:20 PM5/23/18
to Django developers (Contributions to Django itself)
Thanks Collin, that does help.

I guess I am going to have to take a fundamentally different approach to accomplish what I want.

I have attempted to make an AJAX solution for foreign key dropdowns with >1000 options by creating a subclass of ModelChoiceField. 

By also subclassing its iterator I was able to prevent the large queryset from being evaluated/rendered so it loads quickly, and because I didn't use limit_choices_to, validation still works. I then use a textbox to catch search terms and fire off the AJAX call will populate the dropdown with the matching data.

However if there is an initial value supplied, this obviously won't be honored as the initial value is not present in the (empty) dropdown.

The dream was to use formfield.initial to create and select the single requested option, however it seems that is a no go.

Perhaps I will have to cave in and use one of the googleable ajax-select solutions.


On Wednesday, May 23, 2018 at 2:38:40 PM UTC-4, Collin Anderson wrote:
Hi Brock,

Yes, that's the intended behavior, and you can see the same behavior on a regular form, without using formsets, which might make it a little more clear what's going on:

class MyForm(forms.Form):
    name = forms.CharField()

form = MyForm(initial={'name': 'test'})
form.initial  #yields {'name':'test'}
form['name'].initial #yields 'test'
form.fields['name'].initial #yields None

The field itself is shared between all instances of MyForm, so passing in initial data to one instance of MyForm shouldn't modify the field. Instead, the BoundField is used to hold the data for a particular form, and as you mention, as the initial value on the boundfield has that value you expect.

Hope this helps,
Collin
 
On Wed, May 23, 2018 at 2:04 PM, Brock Hallenbeck <bhallen...@gmail.com> wrote:
When creating a formset using modelformset_factory and passing in a list of dictionaries to use as initial values, those values can be retrieved from the relevant form, and the relevant boundfield on that form.
However the formfield class itself has no knowledge of that initial data.

Is this intended behavior? I feel like I should be able to catch that initial value in my formfield subclasses.

Here is a dpaste illustrating what I am talking about:

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.

Fabio Caritas Barrionuevo da Luz

unread,
May 24, 2018, 6:26:21 AM5/24/18
to Django developers (Contributions to Django itself)
Reply all
Reply to author
Forward
0 new messages