Set formfield.initial when created using modelformset_factory

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Set formfield.initial when created using modelformset_factory

Brock Hallenbeck
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:

https://dpaste.de/As6Z

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Set formfield.initial when created using modelformset_factory

Collin Anderson-2
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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/CAFO84S7n32FSzz-2JDf3BYVJTc5RdkyzO2%3Dv9YNbCEpnj-9g_g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Set formfield.initial when created using modelformset_factory

Brock Hallenbeck
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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="CU33ptX4CgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">bhallen...@...> 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:

<a href="https://dpaste.de/As6Z" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdpaste.de%2FAs6Z\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFr9wglafh-CZAv7zos6NfIzQLuKQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdpaste.de%2FAs6Z\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFr9wglafh-CZAv7zos6NfIzQLuKQ&#39;;return true;">https://dpaste.de/As6Z

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="CU33ptX4CgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-develop...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="CU33ptX4CgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">django-d...@googlegroups.com.
Visit this group at <a href="https://groups.google.com/group/django-developers" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;">https://groups.google.com/group/django-developers.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/ba0087f2-0ac6-4387-9f16-76112f52526e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Set formfield.initial when created using modelformset_factory

Fabio C. Barrionuevo da Luz
try https://django-autocomplete-light.readthedocs.io/en/master/


Em quarta-feira, 23 de maio de 2018 15:58:20 UTC-3, Brock Hallenbeck escreveu:
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 <[hidden email]> 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:

<a href="https://dpaste.de/As6Z" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdpaste.de%2FAs6Z\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFr9wglafh-CZAv7zos6NfIzQLuKQ&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fdpaste.de%2FAs6Z\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFr9wglafh-CZAv7zos6NfIzQLuKQ&#39;;return true;">https://dpaste.de/As6Z

--
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 [hidden email].
Visit this group at <a href="https://groups.google.com/group/django-developers" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/group/django-developers&#39;;return true;">https://groups.google.com/group/django-developers.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com?utm_medium=email&amp;utm_source=footer" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/django-developers/1d84a32c-d70a-4d42-8b94-ca19f27ede0e%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/27376ad8-12c0-49e4-af47-97be82f40328%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.