chibisov/drf-extensions

NestedViewSetMixin not working in custom actions

mahyess opened this issue · 0 comments

I don't know if I am not using NestedViewSetMixin properly but queryset filter with parent_lookup__id doesn't work with ModelViewSet's custom actions.

The filter works properly when used in ListModelMixin's list action override, but when used in similar way with custom actions, the filter doesn't seem to work at all.

Here is a simple use case of mine:

class School(models.Model):
    name = models.CharField(max_length=100)

class Student(models.Model):
    school = models.ForeignKey(School)
    name = models.CharField(max_length=100)
school_route = router.register(
    'schools',
    SchoolModelViewSet,
    basename="school"
)
school_route.register(
    'studentss',
    StudentModelViewSet,
    basename="school-student",
    parents_query_lookups=['school__id']
)
class SchoolModelViewSet(ModelViewSet):
    queryset = School.objects.all()
    serializer_class = SchoolSerializer

class StudentModelViewSet(NestedViewSetMixin, ModelViewSet):
    serializer_class = StudentSerializer
    lookup_field = 'id'

    def get_queryset(self):
        return Student.objects.filter()

    def list(self, request, *args, **kwargs):
        serializer = self.get_serializer(self.get_queryset(), many=True)  # works properly
        return Response(serializer.data)

    @action(detail=False, methods=['GET'], name='Get Students')
    def students(self, request, *args, **kwargs):
        queryset = self.get_queryset().filter()   # doesn't work
        serializer = self.get_serializer(queryset, many=True) 
        return Response(serializer.data)

Is it because of something I have been doing wrong, or is it intended action?