Questions tagged [drf-queryset]
127 questions
1
vote
1 answer
request = self.context.get('request') , give me'NoneType' object has no attribute 'data'
I'm trying to exclude certain fields from being displayed in the list view of my serializer. To achieve this, I have overridden the to_representation method.
However, when I attempt to access the 'request' object using request =…

Abbasali Rezaei
- 13
- 3
1
vote
0 answers
Why N+1 problem is enormous in DRF and how can I solve it?
I have site that was powered on vanilla django framework. I created a custom manager for my models to reduce N+1 problem and it worked very well. But when I moved to DRF, I just can't handle it with usual select_related and prefetch_related methods.…

jmur
- 109
- 8
1
vote
2 answers
Update only 2 field in DRF
I have a model and with DRF I want update only 2 field in model but when I update , 2 field are update but the other fields are empty .
@api_view(['GET', 'PUT', 'DELETE'])
def snippet_detail(request, format=None):
try:
requestd =…

amin tavakoli
- 21
- 4
1
vote
0 answers
is it possible to avoid n+1 django query here? If so, how?
I have these models
class Thing1(MyModel):
thing2 = models.OneToOneField(
Thing2, on_delete=models.PROTECT, related_name="super_pack"
)
some_id = models.IntegerField()
class Thing2(MyModel):
name =…

sothrowaway34234234
- 35
- 2
1
vote
1 answer
filter queryset for multiple models in Django
I'm implementing a search feature where I'm matching keys from the description. and also matching media if description and media type of ['mp4','mkv','mov','avi'] match so the condition is satisfied.
So I have tried many methods but didn't find an…

Zain Jack
- 27
- 4
1
vote
1 answer
Django Rest Framework. What is queryset and serializer_class?
Let's say I have the following viewset:
class CategoryViewSet(viewsets.ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
I am wondering what does the queryset and serializer_class do here? I can assume…

HTML
- 93
- 5
1
vote
3 answers
permission to class some fields of ViewSet method Django rest framework
I want AllowAny permission only for the retrieve function. In my ViewSets.
class PostLanguageViewSet(viewsets.ViewSet):
permission_classes = (permissions.AllowAny,)
permission_classes_per_method = {
"retrieve": permission_classes
…

MdHassan413
- 63
- 9
1
vote
1 answer
Want to return current page limit in response in Django Rest Framework
Want to return current page limit current_items_per_page in response.
pagination.py
class CustomPageNumberPagination(pagination.PageNumberPagination):
page_size = 10 # Number of objects to return in one page
page_size_query_param =…

MdHassan413
- 63
- 9
1
vote
1 answer
Query M2M relations in Django
I've the following model:
class Quiz(models.Model):
name = models.CharField(max_length=255)
month = models.DateField()
class Question(models.Model):
title = models.CharField(max_lenght=255)
category =…

Arthur Dayne
- 53
- 4
1
vote
2 answers
Django: use related_name to create a subclass
How can I be able to use related_name to create a child model when the parent model is being created.
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
class…

Newton Karanu
- 408
- 5
- 26
1
vote
4 answers
DRF How to select specific fields to display in a nested serializer relationship? (without additional serializers)
I have a serializer
class CategoryListSerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ["id", "name", "name_en", "about", "parent",]
It is used in two locations:
All Categories API: Used to view rich details…

Ji---
- 115
- 1
- 10
1
vote
0 answers
how to pass null value to just one specific key of the one object using same DRF serializer
I'm just passing the same object 2 times in the same serializer but I want to pass key: "retweet": null in one object and in another object I want to pass some values like some many-to-many field values because key "retweet" is many-to-many field…

Muhammad Talha Abbas
- 39
- 1
- 5
1
vote
1 answer
DRF using prefetch_related
I have two classes Vessels and Components, each vessel has several components.
I just want to fetch all vessels and all their related components in one query, I thought prefretch_related does that trick but in DRF in the api i am only receiving the…

newprogrammer12
- 231
- 1
- 10
1
vote
0 answers
Overriding is_valid in Nested serializer to be able to process child model with same information during update()
I'm trying to make my nested serializer to be able to process updating when I'm only updating for the parent model. The default action in drf is that the serializer cant tell if ur child model is trying to updating or create, there's a solution…

Kentypop
- 51
- 7
1
vote
2 answers
how to convert a QuerySet to a set in django?
I have a query,
name_phonenum = person.objects.value_list('first_name','phone_num')
I want to convert the queryset to a set. Any idea how can do it.

Avishkar Yonjan Tamang
- 97
- 1
- 13