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

63 views
Skip to first unread message

Petar Aleksic

unread,
Aug 6, 2016, 8:42:29 AM8/6/16
to Django users
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

Todor Velichkov

unread,
Aug 6, 2016, 9:41:17 AM8/6/16
to Django users
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.
Reply all
Reply to author
Forward
0 new messages