Using Django Rest Framework to initiate data processing on the backend

I seem to have found a solution though perhaps not ideal.

Here is the view:

class GenerateRosterViewSet(viewsets.ViewSet):

    def list(self, request):
        """Get page."""
        data = {"date": "required"}
        return Response(data, status=status.HTTP_200_OK)

    def create(self, request):
        """Generate roster with given start date."""
        serializer = DateTimeSerializer(
        if serializer.is_valid():
            date = serializer.validated_data["date"]
            result = generate_roster.delay(start_date=date)
            data = {"task": result.task_id}
            return Response(data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def retrieve(self, request, pk=None):
        """Not used."""

    def update(self, request, pk=None):
        """Not used."""

    def partial_update(self, request, pk=None):
        """Not used."""

    def destroy(self, request, pk=None):
        """Not used."""

Here is the serializer:

class DateTimeSerializer(serializers.Serializer):
    """DateTime Serializer."""

    date = serializers.DateTimeField()

    def create(self, validated_data):
        """Create date."""
        return datetime(**validated_data)

    def update(self, instance, validated_data):
        """Update date.""" = validated_data.get("date",
        return instance

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top