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

Improve ListSerializer#9244

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

Draft
felipediel wants to merge1 commit intoencode:master
base:master
Choose a base branch
Loading
fromfelipediel:better-list-serializer

Conversation

felipediel
Copy link

Description

#8979 prevents list serializers from having data and instances with different lengths:

https://github.com/encode/django-rest-framework/pull/8979/files#diff-c33f1f011c9f5cf3ed1357b809ebf2b4ed0b808b227c6c007291bf9b259422ecR612-R617

This design choice breaks some use cases. Let's say we want the list serializer to delete from queryset all elements that are not provided in the data during an update. The queryset will be bigger than the data in this case:

# Example: serializer that will break after #8979classBankBulkUpdateSerializer(serializers.ListSerializer):"""Bank bulk update serializer."""defupdate(self,instance:models.QuerySet[Bank],validated_data:list[OrderedDict],    )->list[Bank]:"""Bulk update banks."""bank_dict= {bank.pk:bankforbankininstance}banks_to_create:list[Bank]= []banks_to_update:list[Bank]= []banks_created:list[Bank]= []banks_updated:list[Bank]= []bank_pks_to_keep:list[int]= []forbank_datainvalidated_data:bank_id=bank_data.get("id")bank=bank_dict.get(bank_id)ifbank_idisnotNoneelseNoneifbankisnotNone:forattr,valueinbank_data.items():setattr(bank,attr,value)banks_to_update.append(bank)bank_pks_to_keep.append(bank.pk)else:bank=Bank(**bank_data)banks_to_create.append(bank)withdb_transaction.atomic():instance.exclude(pk__in=bank_pks_to_keep).delete()ifbanks_to_update:update_fields= ["name","compe","ispb","website"]Bank.objects.bulk_update(banks_to_update,update_fields)banks_updated=banks_to_updateifbanks_to_create:banks_created:list[Bank]=Bank.objects.bulk_create(banks_to_create                )returnsorted(banks_created+banks_updated,key=lambdaa:a.name)

The intention of the original PR is good, it solves the problem of not passing initial data and instances to child serializers correctly. But instead of indexing, it's better to relate the elements by id, so we can have different lengths. This PR addresses the issue.

refs#8926#8979

sevdog reacted with thumbs up emoji
Comment on lines +698 to +700
if not self.instance and self.parent and self.parent.instance:
rel_mgr = get_attribute(self.parent.instance, self.source_attrs)
self.instance = rel_mgr.all() if rel_mgr else None
Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure this is the best place to do this. I'd like to hear the opinion of someone more familiar with this codebase.

Copy link
Member

Choose a reason for hiding this comment

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

can you give a more verbose name to rel_mgr please? also share a bit more of what this would be doing?

@felipedielfelipediel marked this pull request as draftJanuary 27, 2024 07:09
@felipediel
Copy link
Author

felipediel commentedJan 27, 2024
edited
Loading

I just realized there's a side case here. The child may not be a ModelSerializer. I'll leave it as a draft while I think about it, suggestions are welcome.

We could create a new ModelListSerializer class with these changes and have it as the default list_serializer_class of ModelSerializer.@auvipy let me know what you think.

@auvipyauvipy self-requested a reviewJanuary 29, 2024 13:35
Copy link
Member

@auvipyauvipy left a comment

Choose a reason for hiding this comment

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

I think we should have appropriate test cases for this before even trying to fix them

@felipediel
Copy link
Author

Agree, I'll do it

Copy link
Member

@auvipyauvipy left a comment

Choose a reason for hiding this comment

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

as you can see the CI builds are failing, can you please rebase and make it green at least? possibly with very basic test? you can try incremental approach to fix the problem

@felipediel
Copy link
Author

Yes, I will do it. Sorry for the delay, I'm struggling to find time to continue this PR, any help is welcome

This was referencedMar 12, 2024
@staleStale
Copy link

stalebot commentedApr 26, 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 26, 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.

2 participants
@felipediel@auvipy

[8]ページ先頭

©2009-2025 Movatter.jp