Opened 2 years ago
Last modified 23 months ago
#35041 closed Cleanup/optimization
DATA_UPLOAD_MAX_MEMORY_SIZE causes a confusing error when not an integer — at Version 1
| Reported by: | Dimitar Tasev | Owned by: | nobody |
|---|---|---|---|
| Component: | File uploads/storage | Version: | 4.2 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description (last modified by )
When trying to POST to a FileField or ImageField, an error will be shown when trying to save the object (even without specifying a file) if DATA_UPLOAD_MAX_MEMORY_SIZE is not an integer.
To replicate:
# in settings.py
DATA_UPLOAD_MAX_MEMORY_SIZE = 4e7
# in models.py
from django.db import models
class FileHolder(models.Model):
file = models.FileField(upload_to="files", blank=True, null=True)
# in admin,py
from django.contrib import admin
admin.site.register(FileHolder, admin.ModelAdmin)
Make migrations & migrate, then go to the Admin view of the model, create a new instance and save. There is no need to specify any file for upload, the error will be shown, here is a stacktrace with Django 4.2.8 and Python 3.11.6
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/contrib/admin/options.py", line 688, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/utils/decorators.py", line 130, in _wrapper_view
result = middleware.process_view(request, view_func, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/middleware/csrf.py", line 470, in process_view
self._check_token(request)
File "/usr/local/lib/python3.11/site-packages/django/middleware/csrf.py", line 373, in _check_token
request_csrf_token = request.POST.get("csrfmiddlewaretoken", "")
^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/core/handlers/wsgi.py", line 93, in _get_post
self._load_post_and_files()
File "/usr/local/lib/python3.11/site-packages/django/http/request.py", line 373, in _load_post_and_files
self._post, self._files = self.parse_file_upload(self.META, data)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/http/request.py", line 321, in parse_file_upload
return parser.parse()
^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/http/multipartparser.py", line 123, in parse
return self._parse()
^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/http/multipartparser.py", line 235, in _parse
data = field_stream.read(size=read_size)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/http/multipartparser.py", line 465, in read
return b"".join(parts())
^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/django/http/multipartparser.py", line 460, in parts
emitting = chunk[:remaining]
~~~~~^^^^^^^^^^^^
TypeError: slice indices must be integers or None or have an __index__ method
Changing DATA_UPLOAD_MAX_MEMORY_SIZE = 4e7 to DATA_UPLOAD_MAX_MEMORY_SIZE = 40000000 and repeating the steps will remove the error.
As far as I can tell Django doesn't do type checking of the value of the settings, at least I couldn't get other settings to fail due to invalid types. If a type check during the "system check" step is not possible then an additional type assertion would be good so that a better error message can be shown as only integer works.
I have only tested this through Django Admin, but the error happens inside django.http so perhaps it can be replicated via other ways of POST-ing.
Related links:
- StackOverflow question where I found the solution first: https://stackoverflow.com/questions/48865441/django-admin-typeerror-on-any-post-request