简体   繁体   中英

how to override 'get_queryset()' in Django

I'm going to use the generic view in django. I defined the serializer_class and override the get_queryset() method, but there is an error telling me to override the get_queryset() method. I wonder how my override method is wrong, and what I have to do to solve the error. Here is my codes.

views.py

from .models import arduino
from .serializers import arduinoToAndroidSerializers, arduinoToDatabaseSerializers
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework.generics import ListCreateAPIView

class arduinoToAndroidViewSet (ViewSet) :
    def dataSend (self, request) :
        user = self.request.user
        queryset = arduino.objects.filter(name=user)
        serializer = arduinoToAndroidSerializers(queryset, many=True)
        return Response(serializer.data)

class arduinoToDatabaseViewSet (ListCreateAPIView) :
    serializer_class = arduinoToDatabaseSerializers
    def dataReceive (self, request) :
        user = self.request.user
        queryset = self.get_queryset()
        queryset = arduino.objects.filter(queryset, name=user)
        serializer = arduinoToDatabaseSerializers(queryset, many=True)
        return Response(serializer.data)

serializers.py

class arduinoToAndroidSerializers (serializers.ModelSerializer) :
    name = serializers.CharField(source='name.username')
    class Meta :
        model = arduino
        fields = ('name', 'temp', 'humi')

class arduinoToDatabaseSerializers (serializers.ModelSerializer) :
    class Meta :
        model = arduino
        fields = ('temp', 'humi')

Besides this, if you see improvement point in my code, please give me tips

You can override the queryset like this. See here in the docs for more info.

class arduinoToDatabaseViewSet (ListCreateAPIView) :
    serializer_class = arduinoToDatabaseSerializers
    
    
        user = self.request.user
        return arduino.objects.filter(name=user)
       

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM