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

Error importing mixins using from rest_framework_bulk import mixins as bulk_mixins #2

Closed
ldgarcia opened this issue Mar 29, 2014 · 4 comments · Fixed by #5
Closed

Comments

@ldgarcia
Copy link

Background
I needed to write my own view, using the provided bulk mixins.

Problem
I first tried importing using from rest_framework_bulk import mixins as bulk_mixins. Then, got the following error AttributeError: 'module' object has no attribute 'BulkCreateModelMixin' when trying to use bulk_mixins.BulkCreateModelMixin.

In the Django shell:

>>> from rest_framework_bulk import mixins as bulk_mixins
>>> dir(bulk_mixins)
['CreateModelMixin', 'DestroyModelMixin', 'Http404', 'ListModelMixin', 'Response', 'RetrieveModelMixin', 'UpdateModelMixin', 'ValidationError', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_get_validation_exclusions', 'api_settings', 'clone_request', 'status', 'unicode_literals', 'warnings']

Whereas:

>>> from rest_framework_bulk.generics import bulk_mixins
>>> dir(bulk_mixins)
['BulkCreateModelMixin', 'BulkDestroyModelMixin', 'BulkUpdateModelMixin', 'CreateModelMixin', 'Response', 'ValidationError', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'print_function', 'status', 'unicode_literals']

Solution

from rest_framework_bulk.generics import bulk_mixins

Having fixed the above problem, I stumped with the following:

  1. Make a POST request passing an array of objects.
  2. It saves correctly, but the response throws this error: 'RelationsList' object has no attribute 'priority'.
  3. After that, make a GET request and the previously updated data renders correctly.

When a non-bulk POST request (with just one object, not a list of objects) is made, it saves successfully and the response throws no error.

In my view I have:

      def get_serializer_class(self):
          if self.request.method == 'GET':
              return NestedSelectedPlaceSerializer
          return SelectedPlaceSerializer

My serializer classes are:

class SelectedPlaceSerializer(serializers.ModelSerializer):
    class Meta:
        model =  SelectedPlace
        fields = ('user', 'place', 'priority')

class NestedSelectedPlaceSerializer(serializers.ModelSerializer):
    place = PlaceSerializer()

    class Meta:
        model = SelectedPlace
        fields = ('place', 'priority')

Traceback

Environment:


Request Method: POST
Request URL: https://127.0.0.1:8443/api/places/selected

Traceback:
File "/home/user/venv/project1/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/home/user/venv/project1/local/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/renderers.py" in render
  592.         context = self.get_context(data, accepted_media_type, renderer_context)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/renderers.py" in get_context
  542.         raw_data_post_form = self.get_raw_data_form(view, 'POST', request)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/renderers.py" in get_raw_data_form
  492.                 content = renderer.render(serializer.data, accepted, context)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/serializers.py" in data
  573.                 self._data = self.to_native(obj)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/serializers.py" in to_native
  349.             value = field.field_to_native(obj, field_name)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/fields.py" in field_to_native
  334.         return super(WritableField, self).field_to_native(obj, field_name)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/fields.py" in field_to_native
  198.             value = get_component(value, component)
File "/home/user/venv/project1/local/lib/python2.7/site-packages/rest_framework/fields.py" in get_component
  56.         val = getattr(obj, attr_name)

Exception Type: AttributeError at /api/places/selected/
Exception Value: 'RelationsList' object has no attribute 'priority'
@miki725
Copy link
Owner

miki725 commented Mar 29, 2014

The failing import is very strange. Will try to replicate the issue. As for the other issue, will look over it sometime this week. Thank you for reporting.

@miki725
Copy link
Owner

miki725 commented Mar 30, 2014

Are you using Django REST Api views (the ones with templates, etc). Or you are making the API call directly using curl or POSTMAN, etc?

@ldgarcia
Copy link
Author

Hi @miki725, have you had a chance to take a look at this?

@miki725
Copy link
Owner

miki725 commented Apr 16, 2014

Just merged this and bumped version number

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

Successfully merging a pull request may close this issue.

2 participants