Django-rest-framework: Dropping or simplifying ListField/DictField support for HTML inputs

Created on 4 Jul 2019  路  1Comment  路  Source: encode/django-rest-framework

ListField and DictField support for form data is complicated and buggy, and I'm tempted to remove it entirely, or simplify it to basically just JSON string parsing. The fundamental issue is that while we have some strategies for parsing form data, we don't actually support HTML forms.

For example, HTML forms submit all inputs, so an empty ListField input might be submitted as ?field=, which is parsed by Django as <QueryDict: {'field': ['']}>. The problem being that the field is parsed as a list containing one empty string. However, it's not really possible to know what the user intended. Did they want to skip the field entirely (relying on the field's default), submit an empty list, or submit a list containing the one empty string? All three of these are valid.

Additionally, rendering the form inputs is complicated. Any solution would require a combination of multi-input template rendering, plus some javascript so users could add/remove inputs.

Solutions

First, we could leave the functionality as-is and begin deprecation. No bug fixes will be accepted. Users would eventually need to rely on 3rd-party solutions.

The other option would be to only support JSON strings as inputs. e.g., field= would imply no value/use the field default, field=[] would be the empty list, and field=[""] the list with empty an empty string. Essentially, ListField and DictField would act similarly to JSONField, but would have a minimal schema (list of ints, list of dicts of ints, etc...). This should solve all of the above issues. The only downside I can think of would be that it wouldn't be trivial to deprecate the old behavior (maybe some kind of flag/option?).

Refs

5807, #6009

Most helpful comment

We use ListField and child as ImageField/FileField to accept multiple image upload at once. Would be sad to see it deprecated.

>All comments

We use ListField and child as ImageField/FileField to accept multiple image upload at once. Would be sad to see it deprecated.

Was this page helpful?
0 / 5 - 0 ratings