Django - sometimes request.POST is mutable, sometimes it is not

I am working on some legacy Django code. I have two almost identical views:

@login_required
def foo(request):
    assert False, "foo mutable=%s" % request.POST._mutable

@login_required
def bar(request):
    assert False, "foo mutable=%s" % request.POST._mutable

Strange to _mutableeat Truefor one of the handlers and Falsefor the other.

There is no special middleware, and the stack traces on the final page of Django's debugging are almost the same.

Of course, I can get around the problem by using request.POST.copy()or request.POST._mutable = Trueto make the / a object QueryDictchange, but I would like to know what might be causing this.

+3
source share
1 answer

False, Django, True, MultiPartParser.parse, , CONTENT_TYPE multipart.

_load_post_and_files HttpRequest:

if self.META.get('CONTENT_TYPE', '').startswith('multipart'):
    self._raw_post_data = ''
    try:
        self._post, self._files = self.parse_file_upload(self.META, self)
        ...

parse_file_upload:

parser = MultiPartParser(META, post_data, self.upload_handlers, self.encoding)
return parser.parse()

MultiPartParser.parse:

self._post = QueryDict('', mutable=True)
...
return self._post, self._files

, , - , .

+4

Source: https://habr.com/ru/post/1789113/


All Articles