Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested serializers #2

Open
creynold opened this issue Oct 7, 2015 · 2 comments
Open

Nested serializers #2

creynold opened this issue Oct 7, 2015 · 2 comments

Comments

@creynold
Copy link

creynold commented Oct 7, 2015

Is there currently a way to utilize nested serializers within drf-cached-instances? When I try to use cachedviewsetmixin on my viewset I get the following error:

Got AttributeError when attempting to get a value for field `type` on serializer `NestedSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the 
`PkOnlyModel`   instance.
Original exception text was: 'PkOnlyModel' object has no attribute 'type'.
@jwhitlock
Copy link
Owner

I don't used nested serializers myself, but something like this would work to turn a PkOnlyModel into a CachedModel:

cached_model = CachedQueryset(Cache(), OrigModel.objects.all()).get(pk=pk_only_model.pk)

Could you post the full traceback? There may be a DRF method that cachedviewsetmixin should override to make this happen without the explicit ugliness.

@creynold
Copy link
Author

creynold commented Oct 7, 2015

I'll try your suggestion, in the meantime here's the full traceback:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/test_runs/

Django Version: 1.8.4
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'oauth2_provider',
 'rest_framework',
 'djoser',
 'corsheaders',
 'app.api',
 'rest_framework_swagger',
 'debug_toolbar')
Installed Middleware:
(u'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  125.                     response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/debug_toolbar/middleware.py" in process_view
  82.             response = panel.process_view(request, view_func, view_args, view_kwargs)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/debug_toolbar/panels/profiling.py" in process_view
  134.         return self.profiler.runcall(view_func, *args, **view_kwargs)
File "/usr/local/Cellar/python/2.7.10_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/cProfile.py" in runcall
  149.             return func(*args, **kw)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
  87.             return self.dispatch(request, *args, **kwargs)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)
File "/Users/creynold/Documents/sie-nvme-api/app/api/views.py" in list
  619.         return super(TestRunViewSet, self).list(*args, **kwargs)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/mixins.py" in list
  45.             return self.get_paginated_response(serializer.data)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  663.         ret = super(ListSerializer, self).data
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  228.                 self._data = self.to_representation(self.instance)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
  603.             self.child.to_representation(item) for item in iterable
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
  461.                 ret[field.field_name] = field.to_representation(attribute)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
  452.                 attribute = field.get_attribute(instance)
File "/Users/creynold/Documents/sie-nvme-api/env/lib/python2.7/site-packages/rest_framework/fields.py" in get_attribute
  418.             raise type(exc)(msg)

Exception Type: AttributeError at /test_runs/
Exception Value: Got AttributeError when attempting to get a value for field `type` on serializer `NestedSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `PkOnlyModel` instance.
Original exception text was: 'PkOnlyModel' object has no attribute 'type'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants