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
620 stars 43 forks source link

AttributeError: 'NestedSerializer' object has no attribute 'is_replaceable' #195

Closed sommelon closed 3 years ago

sommelon commented 3 years ago

I'm getting the above error when I try to create nested objects.

My serializers:

class ContactInfoSerializer(DynamicFieldsMixin, NestedModelSerializer):
    phones = NestedField(PhoneSerializer, many=True, required=False, allow_null=True)
    emails = NestedField(EmailSerializer, many=True, required=False, allow_null=True)
class PhoneSerializer(DynamicFieldsMixin, NestedModelSerializer):
    phone_type = NestedField(PhoneTypeSerializer)
    class Meta:
        model = Phone
        fields = '__all__'
class PhoneTypeSerializer(DynamicFieldsMixin, serializers.ModelSerializer):
    class Meta:
        model = PhoneType
        fields = '__all__'

This is the request:

{
   "contact_info":{
      "phones":{
         "create":[
            {
               "phone":"21412214124",
               "phone_type":{
                  "name":"Mobile"
               }
            }
         ]
      },
      "emails":{
         "create":[
            {
               "email":"sda@fe.cs"
            }
         ]
      }
   },
   "name":"string211111",
   "opening_date":"2021-01-21",
   "description":"string",
   "is_active":true
}

It fails when it reaches the phone_type field. Is it because I'm trying to create the PhoneType object in the create operation which is supposed to create Phone?

Full traceback:

Traceback (most recent call last):
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\generics.py", line 242, in post
    return self.create(request, *args, **kwargs)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\mixins.py", line 19, in create
    self.perform_create(serializer)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\mixins.py", line 24, in perform_create
    serializer.save()
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\serializers.py", line 205, in save
    self.instance = self.create(validated_data)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django_restql\mixins.py", line 702, in create
    fields["foreignkey_related"]["writable"]
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django_restql\mixins.py", line 585, in create_writable_foreignkey_related
    obj = serializer.save()
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\serializers.py", line 205, in save
    self.instance = self.create(validated_data)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django_restql\mixins.py", line 710, in create
    fields["many_to"]["many_related"]
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django_restql\mixins.py", line 649, in create_many_to_many_related
    pks = self.bulk_create_objs(field, values[operation])
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django_restql\mixins.py", line 605, in bulk_create_objs
    obj = serializer.save()
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\rest_framework\serializers.py", line 205, in save
    self.instance = self.create(validated_data)
  File "fasdfdsafdasfsda\backend\.venv\lib\site-packages\django_restql\mixins.py", line 678, in create
    if field_serializer.is_replaceable:
AttributeError: 'NestedSerializer' object has no attribute 'is_replaceable'
[21/Jan/2021 21:57:09] "POST /api/companies/ HTTP/1.1" 500 25677
calag4n commented 3 years ago

I currently have the same issue.

yezyilomo commented 3 years ago

I have managed to reproduce the bug, am working on a fix.