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 instanceclass PanelSerializer(serializers.ModelSerializer): class Meta: model = Panel fields = ('id', 'orientation', 'angle', 'meting', 'midpoint_coordinates')
--
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.
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.
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.