I'm using DRF's example of multiple updates which works fine except every self.child.update
is a separate update query to the database.
Is there a way I can rewrite this to call the updates as one query as a bulk update?
class BookListSerializer(serializers.ListSerializer):
def update(self, instance, validated_data):
book_mapping = {book.id: book for book in instance}
data_mapping = {item['id']: item for item in validated_data}
ret = []
for book_id, data in data_mapping.items():
book = book_mapping.get(book_id, None)
ret.append(self.child.update(book, data))
return ret
class BookSerializer(serializers.Serializer):
id = serializers.IntegerField()
class Meta:
list_serializer_class = BookListSerializer