Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Return None values as None in ListSerializer.to_representation#9386

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ericls wants to merge1 commit intoencode:master
base:master
Choose a base branch
Loading
fromericls:list-serializer-none-handling

Conversation

ericls
Copy link

Description

I recently encountered some unexpected behavior when using ListSerializer when there areNone values in the list.

Here's a snippet that describes the issue and provides a way to reproduce it:

fromrest_frameworkimportserializersclassBar(serializers.Serializer):a=serializers.ListSerializer(child=serializers.IntegerField(allow_null=True)    )classFoo(serializers.Serializer):a=serializers.IntegerField(allow_null=True)b=Bar()foo=Foo(data={'a':1,'b': {'a': [None]}})print(foo.is_valid(raise_exception=True))# this passesprint(foo.data)# this triggers the error

traceback:

File~/workspace/django-rest-framework/rest_framework/serializers.py:714,in<listcomp>(.0)709# Dealing with nested relationships, data can be a Manager,710# so, first get a queryset from the Manager if needed711iterable=data.all()ifisinstance(data,models.manager.BaseManager)elsedata713return [-->714self.child.to_representation(item)foriteminiterable715 ]File~/workspace/django-rest-framework/rest_framework/fields.py:923,inIntegerField.to_representation(self,value)922defto_representation(self,value):-->923returnint(value)TypeError:int()argumentmustbeastring,abytes-likeobjectoranumber,not'NoneType'

Another thing to note is thatListField already handles it properly (in#4118 ), in fact I'm just copying what's been done toListField toListSerializer.

So I think there are two issues that kinda throws me off:

  1. If a serializer instance passes the validation, it should not then throw an error when getting the.data attribute.
  2. The behavior should be the same asListField

@@ -711,7 +711,7 @@ def to_representation(self, data):
iterable = data.all() if isinstance(data, models.manager.BaseManager) else data

return [
self.child.to_representation(item) for item in iterable
self.child.to_representation(item)if item is not None else Nonefor item in iterable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

we will need extensive tests for changes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

absolutely. I didn't expect this to be merged as-is, just want to start the conversation.

In terms of tests, what kind of test do we want? I'm thinking just adding new tests for the new behavior.

I think the tricky thing is that we don't know if the current behavior is relied upon, because there's no existing tests for that, do you know if there's a way to get that infromation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

please refer to this comment#9386 (comment)

@sevdog
Copy link
Contributor

Another thing to note is thatListField already handles it properly (in#4118 ), in fact I'm just copying what's been done toListField toListSerializer.

Since the use case is already handled inListField, I belive that it would be better havingListSerializer raising an error if its child is not aSerializer, suggesting to useListField instead.

What is the point of usingListSerialier instead ofListField in this case?

auvipy and DhavalGojiya reacted with thumbs up emoji

@staleStale
Copy link

stalebot commentedApr 27, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stalestalebot added the stale labelApr 27, 2025
@auvipyauvipy removed the stale labelApr 28, 2025
@staleStale
Copy link

stalebot commentedJun 27, 2025

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stalestalebot added the stale labelJun 27, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@auvipyauvipyauvipy requested changes

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@ericls@sevdog@auvipy

[8]ページ先頭

©2009-2025 Movatter.jp