You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created an override for WritableNestedModelSerializer.to_internal_value() that I think is generalizable. This hasn't been exhaustively tested, so use at your own risk.
def to_internal_value(self, data):
def get_object(field, model, serializer, value):
nested_obj = get_object_or_None(model, id=value)
if nested_obj is not None:
obj = serializer(nested_obj, context=self.context).data
for key in list(serializer(nested_obj, context=self.context).data):
if key not in ['id'] + [field_name for field_name, field in serializer().fields.items() if field.required]:
obj.pop(key, None)
return obj
else:
return value
for f in list(data):
if issubclass(self.fields[f].__class__, serializers.Serializer):
if isinstance(data[f], int):
data[f] = get_object(f, self.fields[f].Meta.model, self.fields[f].__class__, data[f])
elif issubclass(self.fields[f].__class__, serializers.ListSerializer):
for idx, item in enumerate(data[f]):
if isinstance(item, int):
data[f][idx] = get_object(f, self.fields[f].child.Meta.model, self.fields[f].child.__class__, item)
return super().to_internal_value(data)
The first request return:
And the second raise TypeError: argument of type 'int' is not iterable
The first request works fine when I do patch/put
The text was updated successfully, but these errors were encountered: