Questions tagged [django-rest-viewsets]
362 questions
2
votes
1 answer
Getting django.db.utils.OperationalError: sub-select returns 11 columns - expected 1 when trying to add an object to a m2m field
I am new to django rest framework, and I am trying to build api for a todo app using which i can add collaborator to a todo my todo model is as follows:
class Todo(models.Model):
creator = models.ForeignKey(User, on_delete=models.CASCADE)
title =…

Purushottam Tiwari
- 31
- 6
2
votes
0 answers
Handle many ViewSets with one URL in the Django rest framework?
What I want is to handle three ViewSet with one URL respect to the provided request.data.
Let's make it more clear by diving into code:
Here is my router.py
# vim: ts=4 sw=4 et
from django.conf.urls import url, include
from rest_framework.routers…

Phoenix
- 3,996
- 4
- 29
- 40
2
votes
1 answer
Django Rest Framework custom serializer's ValidationError not working
I am trying to set up a custom login serializer in Django and want a custom response but the default one always show:
{
"username":[
"This field is required."
],
"password":[
"This field is required."
]
}
I tried to…

Henry La
- 346
- 2
- 5
- 14
2
votes
0 answers
Django rest - router is not creating url path, giving 404, and the api webpage has incorrect urls listed
ive created some urls with the default router as per the below:
api.urls
from django.urls import path, include
from rest_framework import routers
from rest_framework_bulk.routes import BulkRouter
from . import views
router =…

AlexW
- 2,843
- 12
- 74
- 156
2
votes
2 answers
Use serializer of model having foreign key to do CRUD on parent table in Django Rest Framework
In my API, I have two models Question and Option as shown below
class Question(models.Model):
body = models.TextField()
class Options(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
option =…

dipesh
- 763
- 2
- 9
- 27
2
votes
1 answer
How does routers and viewsets configure their urls?
I was reading through a long piece of code. And was stuck at how routers and viewsets automatically configure their URLs.
For eg.
the views.py file is:
class UserViewSet(viewsets.ModelViewSet):
authentication_classes =…

Phoenix
- 373
- 1
- 4
- 20
2
votes
1 answer
How to fix this NoReverseMatch exception in Django rest frameworks routers?
I'm working on this Django project for learning and I'm not able to resolve the URL using reverse().
I have tried to understand this concept from online documentations and I'm not able to succeed with it.
I'm using ModelViewSet in my views.py
In my…

Prashanth S.
- 410
- 4
- 19
2
votes
0 answers
django rest framework - Get url to choices
Having the model, serializer, view and urls:
#models.py:
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
class…

resto
- 21
- 1
2
votes
1 answer
DRF ViewSet Returns QuerySet With Empty Values
I have a DRF ViewSet called "QueryCriteriaViewSet" which I'm using in a query builder which allows users to select a field and then select from related criteria. So, for example, a user can select the "reg_status" field and then select from the…

rkm2
- 53
- 1
- 8
2
votes
1 answer
Django Rest change primarykey in URL to use custom key
i have two models Stats and Autor, how i can change primary key to use a unqiue field search by parameter(Subname in Autor model) i thinking about APIView but anyone can show me how use this?
example :…

kowal666
- 61
- 8
2
votes
0 answers
Django Rest Framework list missing records
I have set up a Django Rest Framework (v.1.11.7) Viewset called CustomerViewset. It's as simple as follows:
class CustomerViewSet(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = CustomerSerializer
The serializer…

Erwol
- 1,911
- 2
- 23
- 28
2
votes
1 answer
Django Viewset Pagination throws serialization error
I have something like this:
Paginating Viewset:
class FeedViewSet(ModelViewSet):
queryset = Feed.objects.all()
serializer_class = FeedSerializer
def list(self, request, *args, **kwargs):
paginator =…

waqasgard
- 801
- 7
- 25
2
votes
2 answers
Django REST ViewSet ordering for Null-able columns
Consider a table Person:
| name | wealth |
|------| ------ |
|Abby | 12 |
|Ben | Null |
|Carl | 5 |
|Diane | Null |
We want to sort the rows by wealth, descending, i.e. to get (Abby, Carl, Ben, Diane), but Django's order_by…

Vidak
- 1,083
- 14
- 29
2
votes
0 answers
Filtering the results in the dropdown for foreign key in Django rest framework
I have 3 models. User, process, processmapping. process model has a created_by filed which is a foreign key to user model. processmapping model has processname filed which is a foreign key to process table.
My code is as follows.
models.py
class…

TheAztec
- 43
- 8
2
votes
1 answer
Method GET not allowed in DRF ViewSet when trying to retrieve single resource
I am new Python and Django. I have created ViewSet as follows:
api/views.py
class UserDetails(ViewSet):
"""
CREATE, SELECT, UPDATE OR DELETE
"""
def retrive(self, request, pk):
user = self.get_object(pk)
print(user.query)
user =…

Zalak Parikh
- 450
- 4
- 11