1

i have problem editing my queryset in Django REST framwork. Basically i need to filter my query and return single one, so my DRF view look like this:

class MyModelViewSet(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer

    def get_queryset(self):
        start = self.kwargs['start']
        end = self.kwargs['end']
        qs, created = MyModel.objects.get_or_create(start=start, end=end)
        return qs

So i return single model instance, but when i try to get access true the DRF web interface i get an error 'MyModel' object has no attribute 'model'

Traceback:

File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/rest_framework/viewsets.py" in view
  85.             return self.dispatch(request, *args, **kwargs)
File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
  456.             response = self.handle_exception(exc)
File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/rest_framework/views.py" in dispatch
  444.             self.initial(request, *args, **kwargs)
File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/rest_framework/views.py" in initial
  359.         self.check_permissions(request)
File "/Users/stay-wide-awake/code/env/lib/python3.4/site-packages/rest_framework/views.py" in check_permissions
  304.             if not permission.has_permission(request, self):
File "/Users/stay-wide-awake/code/asp/src/apps/api2/permission.py" in has_permission
  34.         perms = self.get_required_permissions(request.method, queryset.model)

Exception Type: AttributeError at /api/v2/geo/36.606111,-118.062778|36.578581,-120.291994/
Exception Value: 'MyModel' object has no attribute 'model'

Router and all other parts is OK (tested via prints :))

Will be grateful for any hints.

1 Answer 1

4

The object received via MyModel.objects.get_or_create(start=start, end=end) is not a queryset hence you are getting this error.

Django's get_or_create() returns a tuple of (object, created) where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

In your get_queryset() method, you are setting queryset qs to this received object when infact its a model object.

get_queryset() should return a queryset and not a model object which you are doing.

You can do something like:

def get_queryset(self):
    start = self.kwargs['start']
    end = self.kwargs['end']
    mymodel_obj, created = MyModel.objects.get_or_create(start=start, end=end)
    return MyModel.objects.filter(id=mymodel_obj.id) # returns a queryset now
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.