Fix BookListSerializer update example logic to support creation - #9990
Fix BookListSerializer update example logic to support creation#9990JPisOP007 wants to merge 2 commits into
Conversation
|
Logic looks correct overall — nice catch on the Avoid iterating This keeps the same result but only goes through Is there a docs test covering this example? DRF sometimes has tests that verify code examples in the docs actually run correctly. If one exists for this section, it might need updating too — worth checking. Otherwise this is a solid fix for a real bug in the documentation. Thanks for working on it! |
ulugbekbackend
left a comment
There was a problem hiding this comment.
Overall logic is solid — good fix for the KeyError on creation without an id. Left a couple of inline notes.
| book_mapping = {book.id: book for book in instance} | ||
| data_mapping = {item['id']: item for item in validated_data} | ||
| data_mapping = {item['id']: item for item in validated_data if 'id' in item} | ||
| new_items = [item for item in validated_data if 'id' not in item] |
There was a problem hiding this comment.
Nit: this could be a single loop instead of two separate comprehensions, to avoid iterating validated_data twice — e.g.:
data_mapping = {}
new_items = []
for item in validated_data:
if 'id' in item:
data_mapping[item['id']] = item
else:
new_items.append(item)
| # so use a writable field here, rather than the default which would be read-only. | ||
| id = serializers.IntegerField() | ||
| # The id is optional so that new items (without an id) can be created. | ||
| id = serializers.IntegerField(required=False) |
There was a problem hiding this comment.
Makes sense given the new create path.
There was a problem hiding this comment.
🟢 Approval recommended
The documentation example change directly addresses the reported KeyError scenario and remains consistent with DRF ListSerializer usage patterns.
Pull request overview
This PR fixes a documentation bug in the serializers API guide by updating the BookListSerializer.update() example to correctly handle mixed update + create submissions when some list items do not include an id.
Changes:
- Prevent
KeyErrorin the example by buildingdata_mappingonly from items that include anid. - Add explicit handling for “new” items (without
id) by callingself.child.create(...)for each. - Update the example
BookSerializersoidis optional (required=False) to allow validation of create items.
File summaries
| File | Description |
|---|---|
| docs/api-guide/serializers.md | Updates the ListSerializer multiple-update example to support create-without-id inputs without crashing. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
🟡 Changes recommended
Creation and update processing must preserve the submitted item order.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Balanced
| data_mapping = {item['id']: item for item in validated_data if 'id' in item} | ||
| new_items = [item for item in validated_data if 'id' not in item] |
Description
This PR fixes a bug in the documentation example for
BookListSerializer.update()under theListSerializersection of the serializers API guide.Problem
The current documentation example crashes with a
KeyErrorif list items are submitted without anidfield. This occurs when trying to perform creations alongside updates via aListSerializer.Solution
data_mappingto only include validated data items containing an'id'key (updates/deletes).new_itemsthat do not contain an'id'key (creations).new_itemsand called.create(data)on the child serializer (self.child.create(data)).BookSerializerdefinition in the example to useid = serializers.IntegerField(required=False). This ensures validation succeeds when creating new items that do not yet have an ID.refs #9469