yezyilomo / django-restql

Turn your API made with Django REST Framework(DRF) into a GraphQL like API.
https://yezyilomo.github.io/django-restql
MIT License
616 stars 43 forks source link

Wrong data passed to the nested serializer #292

Closed sommelon closed 2 years ago

sommelon commented 2 years ago

Describe what you are trying to accomplish

I'm trying to create a nested object with a foreign key relation.

Relevant models

I'll illustrate the error on the models Phone and Student from the testapp

Relevant serializers

class NestedStudentSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'

class NestedPhoneSerializer(DynamicFieldsMixin, NestedModelSerializer):
    student = NestedField(NestedStudentSerializer)
    class Meta:
        model = Phone
        fields = '__all__'

Request body

{
    "number": "0909090900",
    "type": "personal",
    "student": {
        "name": "John Depp",
        "age": 45,
        "course": 2
    }
}

What's the result?

Data that gets passed to the nested serializer is converted to the native representation and I get this error.

{'course': ['Incorrect type. Expected pk value, received Course.']}

Versions django = "==2.2.24" djangorestframework = "==3.12.2"

I created a fork with a test here

yezyilomo commented 2 years ago

Thank you @sommelon for reporting this, I've confirmed, it's a bug, am working on a fix.

yezyilomo commented 2 years ago

The line causing the bug

https://github.com/yezyilomo/django-restql/blob/d4f21330002fa3645cef1a15215bb3d5a28c2ddb/django_restql/fields.py#L284

It should simply be

    return data
sommelon commented 2 years ago

thanks for the quick release!