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.
score: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
Credit To: stackoverflow.com
Related Query
- QuerySet object has no attribute 'user' on Django Rest Framework
- Django REST framework GET nested serializer produces an error: 'RelatedManager' object has no attribute 'datafile_set'"
- Django REST framework queryset object has no attribute pk
- Django Rest Framework 'RelatedManager' object has no attribute
- Django Request Framework 'ManyRelatedField' object has no attribute 'queryset' when modifying queryset in get_fields
- AttributeError at / 'OrderedDict' object has no attribute 'register' in Django REST framework from quickstart documentation
- Django Rest Framework APIRequestFactory request object has no attribute 'query_params'
- AttributeError 'tuple' object has no attribute 'values' django rest framework
- Django Rest Framework 'list' object has no attribute values
- 'collections.OrderedDict' object has no attribute 'pk' - django rest framework
- Django Rest framework authtoken 'module' object has no attribute 'views'
- Django Rest Framework Test case issue: 'HttpResponseNotAllowed' object has no attribute 'data'
- object has no attribute 'build_absolute_uri' - Django Rest Framework
- Django Rest Framework AttributeError 'function' object has no attribute 'model' in router.register
- Django Rest Framework - 'module' object has no attribute 'HStoreField'
- AttributeError: 'property' object has no attribute 'copy' - while trying to get object list in Django Rest
- Django Rest Framework - object has no attribute post
- AttributeError: 'NoneType' object has no attribute '_default_manager' when running lettuce with django rest framework
- 'collections.OrderedDict' object has no attribute 'uuid' - Django REST Framework
- Django REST Framework - get_queryset: 'MyModel' object has no attribute 'model'
- 'Users' object has no attribute 'values' django rest framework
- Original exception text was: 'QuerySet' object has no attribute 'name' using django rest framework
- Django rest framework error object X has no attribute 'get_extra_actions'
- Django Rest Framework AttributeError 'CustomUser' object has no attribute 'user'
- Python 3 Django Rest Framework - Manager object has no attribute error
- Django Rest Framework - Get related model field in serializer
- django type object Http404 has no attribute get
- Get django object id based on model attribute
- Django calling save on a QuerySet object - 'QuerySet' object has no attribute 'save'
- How can I get the parent object in Django Rest Framework serializer
More Query from same tag
- Django Haystack Whoosh Multilanguage site
- How to add a "unique_together" constraint to a core model in django?
- Dynamically serving django docker containers
- How to set dynamic initial values to django modelform field
- Django: forms and custom templates
- Trying to use CSS in Django Hello World
- type object 'multichoice' has no attribute '_default_manager'
- Django field refering to another field
- Django Angular Facebook authentication
- Can't start gunicorn with systemd django
- How to create a unique random username for django?
- Page not found using Django
- How to get slug for a submit form?
- How to create drag and drop and forward to some function in django?
- Translating MySql query into Django ORM query