如何在 Django 中覆盖 'get_queryset()'
how to override 'get_queryset()' in Django
我将在 Django 中使用通用视图。我定义了 serializer_class 并覆盖了 get_queryset() 方法,但是有一个错误告诉我要覆盖 get_queryset() 方法。我想知道我的 override 方法是怎么错的,我必须做些什么来解决这个错误。这是我的代码。
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')
除此之外,如果您在我的代码中看到改进点,请给我提示
您可以像这样覆盖查询集。有关详细信息,请参阅 docs 中的此处。
class arduinoToDatabaseViewSet (ListCreateAPIView) :
serializer_class = arduinoToDatabaseSerializers
<b>def get_queryset(self) :</b>
user = self.request.user
return arduino.objects.filter(name=user)
</pre>
我将在 Django 中使用通用视图。我定义了 serializer_class 并覆盖了 get_queryset() 方法,但是有一个错误告诉我要覆盖 get_queryset() 方法。我想知道我的 override 方法是怎么错的,我必须做些什么来解决这个错误。这是我的代码。
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')
除此之外,如果您在我的代码中看到改进点,请给我提示
您可以像这样覆盖查询集。有关详细信息,请参阅 docs 中的此处。
class arduinoToDatabaseViewSet (ListCreateAPIView) :
serializer_class = arduinoToDatabaseSerializers
<b>def get_queryset(self) :</b>
user = self.request.user
return arduino.objects.filter(name=user)
</pre>