“Obtenha Queryset” Respostas de código

Passe no Queryset como Django de filtro

users = UserClasses.objects.filter(class_id=data['class_id'])
user_details = User.objects.filter(id__in=users.values_list('id', flat=True))
TitanVentura006

Obtenha Queryset

class PurchaseList(generics.ListAPIView):
    serializer_class = PurchaseSerializer

    def get_queryset(self):
        """
        Optionally restricts the returned purchases to a given user,
        by filtering against a `username` query parameter in the URL.
        """
        queryset = Purchase.objects.all()
        username = self.request.query_params.get('username', None)
        if username is not None:
            queryset = queryset.filter(purchaser__username=username)
        return queryset
Xanthous Xenomorph

Obtenha Queryset

from myapp.models import Purchase
from myapp.serializers import PurchaseSerializer
from rest_framework import generics

class PurchaseList(generics.ListAPIView):
    serializer_class = PurchaseSerializer

    def get_queryset(self):
        """
        This view should return a list of all the purchases
        for the currently authenticated user.
        """
        user = self.request.user
        return Purchase.objects.filter(purchaser=user)
Xanthous Xenomorph

Respostas semelhantes a “Obtenha Queryset”

Perguntas semelhantes a “Obtenha Queryset”

Mais respostas relacionadas para “Obtenha Queryset” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código