Missing primary keys on validated data in ListSerializer subclass .update method

1,995 views
Skip to first unread message

Martin Cavoj

unread,
Feb 10, 2015, 2:43:56 AM2/10/15
to django-res...@googlegroups.com

Hi,

With respect to the issue #2320, the ID field is supposedly to be left out unless it's explicitly set to read_only=False. Does it mean I have to always set it when I want to subclass .update method and define behaviour for a ListSerializer? Otherwise I don't see how this example from API Guide could work (data_mapping):

class BookListSerializer(serializers.ListSerializer):
    def update(self, instance, validated_data):
        # Maps for id->instance and id->data item.
        book_mapping = {book.id: book for book in instance}
        data_mapping = {item['id']: item for item in validated_data}
.....

I think it would make more sense to include the ID field in validated_data in ListSerializer despite being read_only.


Thanks for the clarification.

Silvio Gutierrez

unread,
Nov 12, 2015, 11:11:23 PM11/12/15
to Django REST framework
I've also run into this issue. Seems like the example in the docs won't work as is.

Perhaps the Meta needs to list an id somehow? Or in the BookSerializer?

Tom Christie

unread,
Nov 16, 2015, 8:59:49 AM11/16/15
to Django REST framework
Believe that to be a documentation error - we should be listing the primary key explicitly in that case, so as to not be using the default read-only case. Pull Requests most welcome.

Silvio J. Gutierrez

unread,
Nov 17, 2015, 7:12:16 PM11/17/15
to django-res...@googlegroups.com

On Mon, Nov 16, 2015 at 8:59 AM, Tom Christie <christ...@gmail.com> wrote:
Believe that to be a documentation error - we should be listing the primary key explicitly in that case, so as to not be using the default read-only case. Pull Requests most welcome.

--
You received this message because you are subscribed to a topic in the Google Groups "Django REST framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/01ra1-h_J3w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages