Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Nested field throwing errors #12

@kennell

Description

@kennell

Hi there, using the example provided in the README, i was trying the following:

My Django models:

# models.py

class Artist(models.Model):
    name = models.CharField(max_length=140)


class Album(models.Model):
    title = models.CharField(max_length=140)
    release_date = models.DateField()
    artist = models.ForeignKey(Artist)

The Marshmallow Schemas and DRF Viewsets:

# views.py

class ArtistSchema(Schema):
    name = fields.String()


class AlbumSchema(Schema):
    title = fields.String()
    release_date = fields.Date()
    artist = fields.Nested(ArtistSchema)


class ArtistViewSet(viewsets.ModelViewSet):
    queryset = Artist.objects.all()
    serializer_class = ArtistSchema


class AlbumViewSet(viewsets.ModelViewSet):
    queryset = Album.objects.all()
    serializer_class = AlbumSchema

Finally, my URL configuration:

# urls.py

router = routers.DefaultRouter()
router.register(r'artists', ArtistViewSet, base_name='artists')
router.register(r'albums', AlbumViewSet, base_name='albums')


urlpatterns = [
    url(r'^api/', include(router.urls)),
]

When sending a HTTP GET to /api/albums/ i am geting the following trace:

Django version 1.10.5, using settings 'songify.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /api/albums/
Traceback (most recent call last):
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/django/core/handlers/exception.py", line 39, in inner
    response = get_response(request)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/viewsets.py", line 83, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/mixins.py", line 48, in list
    return Response(serializer.data)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/serializers.py", line 729, in data
    ret = super(ListSerializer, self).data
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/serializers.py", line 647, in to_representation
    self.child.to_representation(item) for item in iterable
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/serializers.py", line 647, in <listcomp>
    self.child.to_representation(item) for item in iterable
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_marshmallow/__init__.py", line 27, in to_representation
    return self.dump(instance).data
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/schema.py", line 511, in dump
    **kwargs
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/marshalling.py", line 147, in serialize
    index=(index if index_errors else None)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/marshalling.py", line 68, in call_and_store
    value = getter_func(data)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/marshalling.py", line 141, in <lambda>
    getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/fields.py", line 252, in serialize
    return self._serialize(value, attr, obj)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/fields.py", line 444, in _serialize
    schema = self.schema
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/marshmallow/fields.py", line 415, in schema
    dump_only=self._nested_normalized_option('dump_only'))
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_marshmallow/__init__.py", line 23, in __init__
    super(Schema, self).__init__(*args, **kwargs)
  File "/Users/k/.virtualenvs/songify/lib/python3.6/site-packages/rest_framework/serializers.py", line 118, in __init__
    super(BaseSerializer, self).__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'load_only'
[21/Feb/2017 22:06:44] "GET /api/albums/ HTTP/1.1" 500 16063

When i remove the artist = fields.Nested(ArtistSchema) from the AlbumSchema class, i receive a valid response without any errors. This is on Python 3.6, using the following package versions:

appdirs==1.4.0
Django==1.10.5
django-rest-marshmallow==2.0.0
djangorestframework==3.5.4
marshmallow==2.13.0
packaging==16.8
pyparsing==2.1.10
six==1.10.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions