4

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

leedjango
  • 411
  • 2
  • 9
  • 18
  • As far as I can see, you have not overridden the get_queryset method. You can read about [overriding the initial queryset in the DRF docs](https://www.django-rest-framework.org/api-guide/filtering/#overriding-the-initial-queryset). You might also want to take a look in the [DRF ViewSets documentation](https://www.django-rest-framework.org/api-guide/viewsets/), since you haven't implemented methods such as `list` for your ViewSet. If you haven't done DRF's official tutorial, this [supplementary guide](https://learndjango.com/tutorials/official-django-rest-framework-tutorial-beginners) can help. – datalowe Jul 19 '20 at 06:47
  • Also, Python naming conventions suggest capitalizing the first letter of classes. So to follow the conventions you'd use e. g. `Arduino` and `ArduinoToDatabaseSerializers`. This makes your code a bit more readable. – datalowe Jul 19 '20 at 06:49

2 Answers2

10

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

class arduinoToDatabaseViewSet (ListCreateAPIView) :
    serializer_class = arduinoToDatabaseSerializers
    
    def get_queryset(self) :
        user = self.request.user
        return arduino.objects.filter(name=user)
       
arjun
  • 7,230
  • 4
  • 12
  • 29
0

If you want filter data by some category, tags, slug etc.

urls.py

urlpatterns = [
    path('blogs/filter/by/category/<str:category_name>/', views.FilterByCategory, name='some_name'),
    path('blogs/filter/by/tags/<str:category_name>/', views.FilterByTags, name='some_name'),
]

views.py

class FilterByCategory(ListView):
   queryset         = Blogs.objects.all()
   serializer_class = BlogsSerializer

   
    
   def get_queryset(self):

       cat_name = self.kwargs.get('category_name', None) 
       blogs = self.queryset.filter(
                Q(category__name = cat_name)
              | Q(category__name__istartswith = cat_name)
              | Q(category__name__iendswith = cat_name)  )
      return blogs

By using the same approach you filter out any data from the database.

DaveL17
  • 1,673
  • 7
  • 24
  • 38