Avoid PUT response data to contain full data with nested serializer and generic views

27 views
Skip to first unread message

Jean-Pierre Merx

unread,
Apr 2, 2020, 5:25:13 AM4/2/20
to Django REST framework
Hello,

I'm building an API based on a RetrieveUpdateAPIView with a Writable nested serializers. The issue I have is that when I make a PUT request, the response data contains all the data of the serializer... And the amount of those data is pretty big.

How can I modify my code to avoid this behavior? I imagine that this should be at the level of the view?

Thanks for your support.

For the record,

Here is the serializer code:

# DatetimeValue serializer
class DatetimeValueSerializer(serializers.ModelSerializer):
class Meta:
model = DatetimeValue
fields = ['date_time', 'value']


# Serializer that includes time series header and date time values
class TimeSeriesValueSerializer(serializers.ModelSerializer):
values = DatetimeValueSerializer(many=True)

class Meta:
model = TimeSeries
fields = ['id', 'name', 'values']

def update(self, instance, validated_data):
ts_name = validated_data.pop('name')

try:
time_series = TimeSeries.objects.get(name=ts_name)
except ObjectDoesNotExist:
raise ValidationError(_(f"Time series doesn't exist"))

values_data = validated_data.pop('values')
for datetime_value in values_data:
try:
DatetimeValue.objects.create(time_series=time_series, **datetime_value)
except IntegrityError:
raise ValidationError(_(f"ERROR in 'date_time' and/or 'value' provided. "
f"'date_time' has to be a correct date time format and 'value' a float. "
f"A 'date_time' has to be unique for a given time series"))
return time_series

And the view code:

# View to get time series datetime values
class TimeSeriesValueDetail(generics.RetrieveUpdateAPIView):
queryset = TimeSeries.objects.all()
serializer_class = TimeSeriesValueSerializer
Reply all
Reply to author
Forward
0 new messages