Questions tagged [django-rest-viewsets]

362 questions
2
votes
0 answers

Django Rest Framework - Cache 'list' function but not 'retrieve' function (detail) in ModelViewSet

I have the following code: class OrderListViewSet(viewsets.ModelViewSet, ReadOnlyCacheResponseAndETAGMixin): permission_classes = (NoUpdatePermission,) model_class = Order lookup_field = 'unique_reference' …
1
vote
1 answer

DRF prefetch related still causing N+1 queries

Models: class GradePolicy(models.Model): name = models.CharField(max_length=30) minScore = models.DecimalField(default=0, max_digits=4, decimal_places=1, db_column="minscore") maxScore = models.DecimalField(max_digits=4,…
1
vote
1 answer

How can I serialize a list of objects and return the object if validated?

I have the below in one of my viewset classes: serializer = ResponsesSerializer(data=queryset, many=True) serializer.is_valid(raise_exception=True) validated_data = serializer.validated_data response = Response(validated_data,…
1
vote
0 answers

AssertionError: The field 'uploaded_file' was declared on serializer LessonPlanNotesSerializer, but has not been included in the 'fields' option

My Models class LessonPlan(models.Model): planner=models.ForeignKey('LessonPlanner', on_delete=models.CASCADE) title=models.CharField(max_length=100, null=True, blank=True) details=models.TextField(null=True,…
1
vote
1 answer

Using http methods with django rest framework

I'm looking for some advice on how to correctly use my DRF API I have built, specifically the PATCH method at present. I am trying to formulate a script to patch the quantity of a Product / Cart Item that has been successfully added to the cart but…
1
vote
1 answer

The endpoint is throwing me a KeyError when I try to update it

Pardon me, I'm developing an app using Django REST Framework and when I try to update an user this happen It throw me this error: centros = validated_data.pop('centros') KeyError: 'centros' I'll share my code: This is my model class…
1
vote
1 answer

Django Rest UpdateView: This field is required

I have the following view: class CampaignUpdate(generics.RetrieveUpdateAPIView): queryset = Campaign.objects.all() serializer_class = CampaignSerializer permission_classes = [CampaignDetailPermission] lookup_field = 'cid' And I have…
1
vote
1 answer

Post is working in model view set, with functions name post

class Product_List(viewsets.ModelViewSet): # >>> List of products & add product to cart permission_classes = [IsAuthenticated, ] queryset = Item.objects.all() serializer_class = ProductListSerializer def post(self,…
1
vote
2 answers

How do I specify a custom lookup field for a DRF action on a viewset?

I would like to specify a custom lookup field on the action (different from the viewset default "pk"), i.e. @action( methods=["GET"], detail=True, url_name="something", url_path="something", …
1
vote
1 answer

How to create a statistics endpoint?

I'm learning DRF and I've been stuck on this for a few days. I'm trying to create an endpoint that receives a date range .The response should return a report with the monthly sales distribution for the selected period between date_after and…
1
vote
1 answer

Django DRF serializers, how to add new field, fusion of two models?

I have two models, I have to make an endpoint where the results of two tables should appear in the json, which have a fongeringkey that joins them. My code is the following: models.py class Property(models.Model): address =…
1
vote
2 answers

Django Rest Framework - How to implement a view to return a javascript file?

I need to serve/return javascript files from a Django Rest Framework api, so in any other website client I can do: and import the content from it to my clients…
1
vote
1 answer

Django ViewSet serializer_class is being ignored

I have two models: ModelA and ModelB, with their corresponding serializers ModelASerializer and ModelBSerializer In a specific viewset, called MyViewSet i have the follwing structure: class MyViewSetRoot(viewsets.ModelViewSet): http_method_names…
1
vote
1 answer

How to serialize data more efficiently in Django Rest Framework?

The part of my problem is solved by the question: #72534250 I understood that the path I was following was wrong, and I should change the optimization of the models to serializers and/or viewsets. But how? I had this structure: *I already removed…
1
vote
1 answer

can any one tell how can i get an appropriate output only using a foreign key in models

this is my models.py file models.py from django.db import models from django.utils import timezone class Movielist(models.Model) : Title = models.CharField(max_length=1000) Description = models.TextField(blank=True) ReleaseDate =…