在Django Rest框架中发送上下文数据

时间:2019-09-24 10:57:41

标签: python django django-rest-framework

我在django应用程序中创建了一个日历,因此必须在上下文中传递queryset。这是代码:

def event(request):
    all_events = Quiz.objects.filter(owner_id=request.user, status="Assigned")
    context = {"events": all_events, "get_event_types": all_events, }
    return render(request, 'classroom/teachers/calendar.html', context)

然后在Django模板中同时使用eventsget_events_types

现在我想在Django rest框架中做同样的事情。所以我这样做了:

Views.py

class CalendarEventsView(viewsets.ModelViewSet):
    serializer_class = CalendarSerializer
    queryset = Quiz.objects.all()

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        queryset = queryset.filter(owner_id=request.user, status="Assigned")
        serializer = self.get_serializer(queryset, many=True)

        print("serializer data is", serializer.data)

        return Response(serializer.data)

serializer.py

class CalendarSerializer(serializers.ModelSerializer):
    class Meta:
        model = Quiz
        fields = "__all__"

现在,这将数据格式的查询集发送给我,但是我可以针对“事件”或“ get_event_types”获取它们吗?这样我才能使用它们?

0 个答案:

没有答案