Questions tagged [django-rest-viewsets]
362 questions
3
votes
2 answers
DRF Response content-type set to None during tests
I'm using Django Rest Framework (version 3.6.2) to create REST API. I've defined my viewset that inherits from GenericViewSet and have overriden retrieve method to implement custom behaviour.
class FooViewSet(viewsets.GenericViewSet):
…

mateuszb
- 1,083
- 10
- 20
3
votes
0 answers
Django rest pagination next url is not correct
I have created a Django rest api using ModelViewSet. Deployed in staging and now in production.Production is working fine,but staging has some incorrect "next" url.
See the key "next" in both images, first image is the staging response and second…

Ferose
- 384
- 5
- 13
3
votes
1 answer
By default, do DRF viewsets allow PUT, PARTIAL_UPDATE, DELETE, CREATE, LIST and RETRIEVE?
Suppose I create this viewset:
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
lookup_field = 'username'
and this router:
router.register(r'users', views.UserViewSet)
and this…

SilentDev
- 20,997
- 28
- 111
- 214
2
votes
2 answers
self.request.user not returning in queryset
In my views, queryset is returning all the users when I want it to be only returning the user that is currently logged. I have a get self method which has the serializer set to the user but it is not being used. When I tried get_queryset,…

Jabba the Hutt
- 654
- 3
- 15
2
votes
1 answer
Django REST Framework reverse() not finding match for router's "{basename}-detail"
This question is extremely similar to: How to fix this NoReverseMatch exception in Django rest frameworks routers? but that hasn't been answered/resolved and after a lot of investigating here I am looking for help.
I am trying to build an API with…

veziop
- 65
- 8
2
votes
1 answer
How to apply filters to extra fields from model serializer that are not in the model with django Rest
We have the SecurityEventItem that have the owner taken from another security_location model and returned.
The logic behind not having security_location direct as a foreign key is that we get events with security_location names that are not added…

ElvisM
- 21
- 2
2
votes
0 answers
DRF changes name or ignores "create" route
I'm new to DRF's "viewsets", but I've inherited some problematic code, and am having trouble figuring out what's going on. I have stripped down the code below to isolate the behavior I don't understand.
In my urls.py I have
from my_app.api import…

nttaylor
- 788
- 3
- 11
- 25
2
votes
1 answer
How to call function when post request succeed in Modelviewset
# views.py
import schedule_update.send_the_result
class UpdatedTypeViewSet(viewsets.ModelViewSet):
queryset = Updated_Type.objects.all()
serializer_class = UpdatedTypeSerializer
# Is it correct to code here, if I would like to call…

victor chang
- 23
- 2
2
votes
1 answer
How to send PUT request to ModelViewSet without passing a primary key in the url?
I am particularly interested in using ModelViewSet
for solving the challenge of updating the logged in user's profile. I am using the following definition:
from rest_framework import viewsets
class ProfileRetrieveUpdate(viewsets.ModelViewSet):
…

DaveIdito
- 1,546
- 14
- 31
2
votes
2 answers
'QuerySet' object has no attribute 'pk'
#models
class Student(models.Model):
firstname = models.CharField(max_length=100,default='ll')
lastname = models.CharField(max_length=100,default='fewf')
id_code = models.CharField(max_length=10,default=0,unique=True)
melli =…
user15446178
2
votes
1 answer
DRF Could not resolve URL for hyperlinked relationship using view name on PrimaryKeyRelatedField
I have a frustrating problem with POST requests on a DRF serializer - DRF is, for some reason, going to an incorrect view name, and view_name is not a settable property on PrimaryKeyRelated Field.
Models:
# (the class with the issue)
class…

DeltaG
- 760
- 2
- 9
- 28
2
votes
2 answers
Can you get the request method in a DRF ModelViewSet?
I am building a Django chat app that uses Django Rest Framework. I created a MessageViewSet that extends ModelViewSet to show all of the message objects:
class MessageViewSet(ModelViewSet):
queryset = Message.objects.all()
serializer_class =…

jsiegel
- 35
- 7
2
votes
2 answers
Return id of new Item created with POST using ModelSerializer and ModelViewSet in Django Rest Framework
I'd like to get the new id as a response after making a Post in Django Rest Framework.
Intended function: User gets a json response with the hyperlink to the new item created.
POST
{
"label": "BMW M3",
"price": 5000.00
}
Response:
{
…

Jaco
- 1,564
- 2
- 9
- 33
2
votes
1 answer
Django rest framework viewsets method return HTTP 405 Method Not Allowed
I am developing a cart api where add item in cart and remove. I craete a CartViewSet(viewsets.ModelViewSet) and also create two method in CartViewSet class add_to_cart and remove_from_cart..But when I want to add item in cart using add_to_cart and…

Rabbi hasan
- 340
- 1
- 5
- 14
2
votes
2 answers
DRF Post to ViewSet without writing into Model
I've always written data into database when posting via Django Rest Framework endpoints. This time I would like to process received data and send it somewhere else without writing into DB. I switched from ModelViewSet to ViewSet, I can issue GET…

nekton
- 29
- 1
- 6