Saturday, August 6, 2016

Re: Updating kwargs['data'] in get_form_kwargs (sometimes) raises AttributeError

Show some code how you create these forms.

Django creates QueryDict objects for request.POST and request.GET which are immutable, so mu guess is that some times you are instancing the form with either request.POST or request.GET and another time (when you have an AJAX call) you are instantiating the forms with a simple dict object (created from json.loads) which is mutable.

However, I don't think that it's a good idea to change form.data, whats the purpose behind it? probably there is a better way.

On Saturday, August 6, 2016 at 3:42:29 PM UTC+3, Petar Aleksic wrote:
Hi,

Basically in two different UpdateViews I update the form kwargs dinamically (on the same way):

def get_form_kwargs(self):
        kwargs = super(MyView, self).get_form_kwargs()
        print("KWARGS", kwargs)
        kwargs['data'].update({
            "something": "somevalue"
        })
        return kwargs


But one view raises AttributeError: This QueryDict instance is immutable and another doesn't. 
I might add that views are called with ajax but, as mentioned, no difference in way calls are triggered. 

Anybody has an idea what might be the cause or had a similar problem?

Best

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ce28d8af-8158-4ef0-ac8b-e19fabecbeee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment