Questions tagged [django-rest-viewsets]
362 questions
6
votes
1 answer
How to set a method in Django rest framework's ViewSet to not require authentication
I have viewset like below :
from rest_framework import viewsets
from paas.serializers import UserSerializer
import logging
logger= logging.getLogger(__name__)
class UserViewSet(viewsets.ViewSet):
def list(self,request):
pass
…

amir
- 2,443
- 3
- 22
- 49
5
votes
1 answer
Django Rest Framework: Send full foreign key objects in GET response but accept only foreign ids in POST payload, without two serializers?
Say I've two models:
class Singer((models.Model):
name = models.CharField(max_length=200)
class Song(models.Model):
title = models.CharField(max_length=200)
singer = models.ForeignKey(Singer)
And two serializers like:
class…

Judy T Raj
- 1,755
- 3
- 27
- 41
5
votes
2 answers
Filter on multiple values in Django Rest Framework
I have a model that I want to filter on multiple values.
my model:
class Product(models.Model):
ean = models.CharField(max_length=13, unique=True)
product_id = models.CharField(max_length=20, null=True, blank=True)
product_image =…

PatrickPel
- 69
- 2
- 7
5
votes
1 answer
Related resources on a nested URL in Django Rest Framework
I have two models, Foo and Bar. Bar has a foreign key to Foo. I have a ModelViewSet for Foo and I want the route /api/foos//bars/ to return all the bars related to the given foo.
I know this can be done using actions. For example
class…

Bary12
- 1,060
- 9
- 23
5
votes
0 answers
django-rest-framework, passing a foreign key field as url parameter for lookup
I am trying to search for all serial numbers with a specific sales_order(foreign key) field by providing the sales order as a url parameter to its viewset. I'm not sure why my queryset isn't being displayed by a GET request
This is a new system I…

JSmalls
- 51
- 1
5
votes
2 answers
How to properly return error when overriding .get_queryset()?
I read on DRF doc that you can filter againt query parameters by overriding .get_queryset(). I am looking for the best practice, on what to return, in case the filters are incorrect and where to return an error message.
The doc I referred to is here…

Solal
- 611
- 2
- 9
- 26
5
votes
1 answer
Post request handling in Django Rest framework
I am using Django Rest Framework, currently to pull some data from the backend we are using Get request, but due to URL limit going high we are planning to implement a Post request. To do this firstly the backend Django Rest API has to be made…

kohl
- 566
- 2
- 9
- 21
5
votes
1 answer
DRF json 404 insetad of html page
I would like to replace default django 404 (and other in the future) with the DRF response, as easy as it is possible, just to return (standard DRF response):
{
"detail": "Not found."
}
So I put this code in my url.py file.
from…

Piotr
- 77
- 5
4
votes
1 answer
pass a variable between multiple custom permission classes in drf
I have a base permission class that two ViewSets are sharing and one other permission class each that is custom to each of the ViewSets, so 3 permissions all together, is there a way to pass a specific variable down from the base permission class to…

hello
- 1,168
- 4
- 24
- 59
4
votes
2 answers
DRF reverse action url from viewset
I have an issue reversing the URL of ViewSet actions in the DRF
my codes are below, I try some methods to reverse URLs but also you can see it's not working for me
view.py
class Device_API(ViewSet):
def list(self, request) -> Response:
…

Nova
- 413
- 4
- 11
4
votes
1 answer
How to Foreign_Key value instead of id in django rest framework without read_only=True
I working on project with drf where I'm getting serializer data as follows which is absolutely fine:
{
"message": "Updated Successfully",
"status": 200,
"errors": {},
"data": {
"id": 8,
"user": 2,
"item": 1,
…

Rajat Jog
- 197
- 1
- 10
4
votes
1 answer
How to override POST method for bulk adding - Django Rest Framework
I'm writing a REST API using Django Rest Framework and I would like that one of my routes accepts bulk adding on POST method, to create multiple objects. Others methods (GET, PUT, PATCH, DELETE) will still remain the same, accepting only one at a…

Luc
- 393
- 7
- 19
4
votes
2 answers
How to use ListSerializer with a ModelSerializer?
I am attempting to create a POST endpoint using DRF ListSerializer to create a list of LogLevel objects.
I have tried to serialize the foreign key using PrimaryKeyRelatedField without success.
models.py
relevant fields for LogLevel model. note…

bbmhmmad
- 372
- 1
- 6
- 21
4
votes
2 answers
django-rest-framework custom viewset retrive with multiple lookup args
I have two models say A and B.
models look like
class A(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000)
slug = models.CharField(max_length=100)
class B(models.Model):
name =…

Shakil
- 4,520
- 3
- 26
- 36
4
votes
2 answers
Django rest ModelViewSet multiple GET requests with different URLs
I have to Models: A Library can have many Books.
Right now I have a URL for performing CRUD on Books in a specific Library:
router.register(r'books/(?P[0-9]+)', BookViewSet, base_name='books')
and corresponding view:
class…

Thinker
- 5,326
- 13
- 61
- 137