I would like to have a nested object inside the serializer, not just foreignkey (or url). As in this documentation , I just needed to specify the serializer class of the nested object in the parent serializer:
This works very well when I receive objects, but it is no longer possible to create (= POST ) Sample objects, I get an error:
{u'non_field_errors': [u'Invalid data']}
I tried to overwrite the create method in the view to get the object using pk:
class SampleViewSet(viewsets.ModelViewSet): queryset = api_models.Sample.objects.all() serializer_class = api_serializers.SampleSerializer def create(self, request): request.DATA['nested'] = get_object_or_404(api_models.NestedSample, pk=request.DATA['nested']) return super(SampleViewSet, self).create(request)
But that does not work.
Any idea?
I also found this question . I can relate this to the fact that, of course, solves the problem, but do not let me expose the full nested object, so let's return to the beginning.
Thanks,
serialization django-rest-framework nested
db0
source share