-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
ListField default values not honored in multipart/form-data mode #5807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Okay. One sensible thing to do would be to try that in the repo, and see which (if any) tests fail, |
That reference is to this PR. What PR did you mean to link to? In general, |
@rpkilby Its only related to the multipart form posts to api endpoints. I don't use the browsable api for updating (disabled that part, for the queries). I'm making the changes now, and will try to work in a test case and show a PR. So far nothing fails in testing, but I haven't added one for this specific issue for others to evaluate yet. If anyone has feedback on the behavior (optional list is always |
It seems that I came across a similar issue related to multi-part forms. I have the following serializer: class KanbanBoardSerializer(serializers.ModelSerializer):
kanban_board_columns = KanbanBoardColumnSerializer(
read_only=False,
many=True,
required=False
)
class Meta:
model = KanbanBoard
fields = (
'pk', 'title', 'kanban_board_columns', 'some_file'
) I then use a multi-part form (to upload to the file-field Obviously, within the serializer I need to handle the def update(self, instance, validated_data):
"""
Override update method of KanbanBoardSerializer such that we can handle sub-serializers for
- kanban_board_columns
:param instance: the instance that is being updated
:param validated_data: validated data of the serializer
:return: KanbanBoard
"""
kanban_board_columns = None
# get kanban board columns from validated data (if it is available)
if 'kanban_board_columns' in validated_data:
kanban_board_columns = validated_data.pop('kanban_board_columns')
# handle inserts/updates/removes on kanban board columns
# update kanban board instance
instance = super(KanbanBoardSerializer, self).update(instance, validated_data)
return instance The result is that However, when I use JSON, the field is not being populated in |
Hi, this is actually related to #2761. See, the reason for #2761 - The solution (a bit bizarre imo) was to add the following check: if self.field_name not in dictionary:
if getattr(self.root, 'partial', False):
return empty While it fixed the issue it obviously made patching of ListFields impossible for "multipart/formdata", and introduced different behavior for I think that now when |
Is it safe now to remove the partial check from the |
When sending a
multipart/form-data
request to a serializer with aListField
, the default value returned for the field is always the empty list[]
. Anydefault=
passed to field will be ignored due to the way html input is handled in theget_value()
function.json
dataSteps to reproduce
Expected behavior
Actual behavior
[]
Explanation and Workaround
The
ListField.get_value()
function always returns the value ofhtml.parse_html_list
from get_value when that field_name was not passed. There is a check at the top for a missing key, but that only affectspartial
posts. Missing keys on regular POSTs will still be processed normally and return[]
.I'm not sure if it is OK to just remove that check for partial and always return
empty
, but that is what I have done for this version of my workaround. The list fields I use this way are in pure serializers and are only used for validating input. They are not used inModelSerializers
so for my case this is ok.Initial Workaround
The text was updated successfully, but these errors were encountered: