cbertinato via Django REST framework
2018-05-04 14:09:12 UTC
As the subject indicates, I am observing some unexpected behavior when
using a serializer within another.
I'm still working on knocking up a working example, but the setup is as
follows. I have a serializer that has a to_representation method and in
that serializer's update method I am serializing an instance of another
model. Something like:
class ParentSerializer:
class Meta:
model = SomeModel
def to_representation(self, obj):
...
def update(self, instance, validated_data):
serializer = SomeOtherModelSerializer(SomeOtherModel, data=data,
context=self.context, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
If that serializer does not validate and raises an exception, then the
exception handler eventually calls on to_representation to render a
response. What is curious is that I see a call to the ParentSerializer
to_representation with an instance of SomeOtherModel. I only noticed
because the assumption in to_representation is that obj is only ever an
instance of SomeModel, which is throwing other errors.
Just trying to get a bead on whether this is expected behavior, or whether
this is indeed a issue in DRF. In the meantime I'll try to work up a small
example to replicate this issue.
Cheers,
Chris
using a serializer within another.
I'm still working on knocking up a working example, but the setup is as
follows. I have a serializer that has a to_representation method and in
that serializer's update method I am serializing an instance of another
model. Something like:
class ParentSerializer:
class Meta:
model = SomeModel
def to_representation(self, obj):
...
def update(self, instance, validated_data):
serializer = SomeOtherModelSerializer(SomeOtherModel, data=data,
context=self.context, partial=True)
serializer.is_valid(raise_exception=True)
serializer.save()
If that serializer does not validate and raises an exception, then the
exception handler eventually calls on to_representation to render a
response. What is curious is that I see a call to the ParentSerializer
to_representation with an instance of SomeOtherModel. I only noticed
because the assumption in to_representation is that obj is only ever an
instance of SomeModel, which is throwing other errors.
Just trying to get a bead on whether this is expected behavior, or whether
this is indeed a issue in DRF. In the meantime I'll try to work up a small
example to replicate this issue.
Cheers,
Chris
--
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.