Skip to content

fix function names and dotted lookups for use in PrimaryKeyRelatedField #694

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

Merged
merged 5 commits into from
May 18, 2013

Conversation

craigds
Copy link
Contributor

@craigds craigds commented Mar 1, 2013

I've been using this, working fine:

class ExportSerializer(serializers.ModelSerializer):
    #...
    layers = serializers.RelatedField(many=True, source='get_layers', queryset=ExportLayer.objects.all())

This works fine, but then I tried to use serializers.PrimaryKeyRelatedField instead and everything broke:

Traceback:
File "/home/cdestigter/checkout/django/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/cdestigter/checkout/django/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/cdestigter/checkout/django/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs)
File "/home/cdestigter/checkout/django-rest-framework/rest_framework/views.py" in dispatch
  404.             response = self.handle_exception(exc)
File "/home/cdestigter/checkout/django-rest-framework/rest_framework/views.py" in dispatch
  401.             response = handler(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/poppy/exports/views.py" in get
  19.         return Response(serializer.data)
File "/home/cdestigter/checkout/django-rest-framework/rest_framework/serializers.py" in data
  387.                 self._data = [self.to_native(item) for item in obj]
File "/home/cdestigter/checkout/django-rest-framework/rest_framework/serializers.py" in to_native
  279.             value = field.field_to_native(obj, field_name)
File "/home/cdestigter/checkout/django-rest-framework/rest_framework/relations.py" in field_to_native
  226.             return [self.to_native(item.pk) for item in queryset.all()]

Exception Type: AttributeError at /api/v2/exports/
Exception Value: 'function' object has no attribute 'all'

This PR fixes the error.

…ld.field_to_native (they work in RelatedField.field_to_native already)
@tomchristie
Copy link
Member

Yeah, seems right. Fancy pulling together a test case so we don't accidentally regress the behavior at some point in the future?

@tomchristie
Copy link
Member

Marked for attention in DjangoCon sprints. Needs regression test.

@craigds
Copy link
Contributor Author

craigds commented May 18, 2013

turns out this breaks for HyperlinkedRelatedField also, so I'll have a fix that shortly

@craigds
Copy link
Contributor Author

craigds commented May 18, 2013

Actually I was mistaken, the HyperlinkedRelatedField works okay already, but I added tests to ensure it stays that way.

tomchristie added a commit that referenced this pull request May 18, 2013
fix function names and dotted lookups for use in PrimaryKeyRelatedField
@tomchristie tomchristie merged commit 9fe6a10 into encode:master May 18, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants