a***@wearespindle.com
2016-11-10 15:07:29 UTC
Hey there,
I am trying to implement some generic nested saving and I've come a long
way so far. But I'm struggling with this part. I want to reuse the
instances for saving of reverse foreign keys and many to many's etc. but I
can't seem to find the list in the ListSerializer. I followed the docs
<http://www.django-rest-framework.org/api-guide/serializers/#customizing-multiple-update>
and created a custom ListSerializer which inherits from the normal list
serializer, but there is no instance and it's not instantiated with data so
I can't access the data, validated_data or is_valid.
My code simplified looks something like this. It's as bare as possible but
I hope it get's the point across.
class BookListSerializer(serializers.ListSerializer):
def save(self, **kwargs):
# The super breaks because of incomplete instantiating.
# Also kwargs is empty.
# There is only one child here and not a list to be found.
# In (pseudo) code it would look something like this, but there is
no instance:
for book in self.validated_data:
if book.instance:
self.child.update(book)
else:
self.child.create(book)
return my_list
class BookSerializer(serializers.ModelSerializer):
# Some fields.
class Meta:
model = Book
list_serializer_class = BookListSerializer
class WritableNestedSerializer(serializers.ModelSerializer):
# This is my implementation for nested writes.
def update(self, instance, validated_data):
# Here I need to do logic for the saving of books.
# I want to reuse the instances filled in already, like with a
foreign key, which get fetched based on the id field in their
validated_data.
# Otherwise I need to do a query for every id in the validated_data
of this serializer, whilst it already has been done.
for field in validated_data:
if field == relational: # Simplified if check.
data = validated_data.pop(field)
instance_list = self.fields[field].save(data) #
This calls the save of my custom list serializer.
return super(WritableNestedSerializer, self).update(**kwargs)
class AuthorSerializer(WritableNestedSerializer):
# Example author serializer with a relation to books.
books = BookSerializer(many=True)
I would expect a ListSerializer to have some kind of list in it with it's
children (or values).
It seems the ListSerializer reuses a single child for everything?
Also why isn't the ListSerializer not instantiated with data automatically
so you can call the save, validated_data etc.?
But my main question is: how would I get this to work without adding a
bunch of queries?
Thanks in advance for any help!
I am trying to implement some generic nested saving and I've come a long
way so far. But I'm struggling with this part. I want to reuse the
instances for saving of reverse foreign keys and many to many's etc. but I
can't seem to find the list in the ListSerializer. I followed the docs
<http://www.django-rest-framework.org/api-guide/serializers/#customizing-multiple-update>
and created a custom ListSerializer which inherits from the normal list
serializer, but there is no instance and it's not instantiated with data so
I can't access the data, validated_data or is_valid.
My code simplified looks something like this. It's as bare as possible but
I hope it get's the point across.
class BookListSerializer(serializers.ListSerializer):
def save(self, **kwargs):
# The super breaks because of incomplete instantiating.
# Also kwargs is empty.
# There is only one child here and not a list to be found.
# In (pseudo) code it would look something like this, but there is
no instance:
for book in self.validated_data:
if book.instance:
self.child.update(book)
else:
self.child.create(book)
return my_list
class BookSerializer(serializers.ModelSerializer):
# Some fields.
class Meta:
model = Book
list_serializer_class = BookListSerializer
class WritableNestedSerializer(serializers.ModelSerializer):
# This is my implementation for nested writes.
def update(self, instance, validated_data):
# Here I need to do logic for the saving of books.
# I want to reuse the instances filled in already, like with a
foreign key, which get fetched based on the id field in their
validated_data.
# Otherwise I need to do a query for every id in the validated_data
of this serializer, whilst it already has been done.
for field in validated_data:
if field == relational: # Simplified if check.
data = validated_data.pop(field)
instance_list = self.fields[field].save(data) #
This calls the save of my custom list serializer.
return super(WritableNestedSerializer, self).update(**kwargs)
class AuthorSerializer(WritableNestedSerializer):
# Example author serializer with a relation to books.
books = BookSerializer(many=True)
I would expect a ListSerializer to have some kind of list in it with it's
children (or values).
It seems the ListSerializer reuses a single child for everything?
Also why isn't the ListSerializer not instantiated with data automatically
so you can call the save, validated_data etc.?
But my main question is: how would I get this to work without adding a
bunch of queries?
Thanks in advance for any help!
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.