id of a nested serializer is missing

1,064 views
Skip to first unread message

Tino de Bruijn

unread,
Oct 29, 2015, 2:16:07 AM10/29/15
to Django REST framework
I am using a nested serializer as described here: http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers. Creation works fine, but in the update method I cannot get the ids of the related models. The main serializer looks like this:

class StringSerializer(serializers.ModelSerializer):
    panels = PanelSerializer(many=True)

    class Meta:
        model = String
        fields = ('id', 'systeem', 'dak', 'panels')

    def update(self, instance, validated_data):
        panels_data = validated_data.pop('panels')
        old_panel_ids = set(instance.panels.values_list('id', flat=True))

        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()

        panels = []
        for panel_data in panels_data:
            if 'id' in panel_data:
                panel = Panel.objects.get(pk=panel_data['id'])
                for attr, value in panel_data.items():
                    setattr(panel, attr, value)
            else:
                panel = Panel.objects.create(string=instance, **panel_data)
            panels.append(panel)

        new_panel_ids = set([p.id for p in panels])
        remove_ids = old_panel_ids - new_panel_ids
        Panel.objects.filter(id__in=remove_ids).delete()
        return instance

and the nested PanelSerializer looks like this:

class PanelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Panel
        fields = ('id', 'orientation', 'angle', 'meting', 'midpoint_coordinates')


The problem is that there never is an 'id' in validated_data['panels'], while it is passed in the json (and is also present in self.data).

How do I get the id's so I can avoid creating new panels every time?

Xavier Ordoquy

unread,
Oct 29, 2015, 2:32:10 AM10/29/15
to django-res...@googlegroups.com
Hi,

Try to print the serializer to ensure the id isn’t read only.

Regards,
Xavier,
Linovia.

--
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-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tino de Bruijn

unread,
Oct 30, 2015, 8:25:57 AM10/30/15
to django-res...@googlegroups.com
Yeah, that was the case. Wouldn't expect it to be, but I can see the point.

Thanks,


Tino

29 October 2015 at 13:32 via Postbox
Hi,

Try to print the serializer to ensure the id isn’t read only.

Regards,
Xavier,
Linovia.


--
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/deoi7M5nBMs/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.
29 October 2015 at 13:16 via Postbox
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/deoi7M5nBMs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages