django-commons / drf-excel

An XLSX spreadsheet renderer for Django REST Framework.
BSD 3-Clause "New" or "Revised" License
217 stars 40 forks source link

Small example in the readme? #7

Closed stuart-bradley closed 5 years ago

stuart-bradley commented 5 years ago

Having a little trouble figuring out how to get this library to work. It's not quite as simple as:

@action(detail=True, methods=['get'], renderer_classes=(XLSXRenderer,))
def report(self, request, *args, **kwargs):
    instance = self._get_cached_object()

    wb = Workbook()
    ws = wb.create_sheet("Mysheet", 0)
    ws.title = "range names"
    for row in range(1, 40):
        ws.append(range(600))

    return Response(wb)

>> KeyError: 'Worksheet 0 does not exist.'

So a little guidance would be appreciated for those of us who can't figure it out!

FlipperPA commented 5 years ago

The README shows to define it as a renderer in the parent class, for example:

class UserViewSet(XLSXFileMixin, viewsets.ModelViewSet):
    """
    A viewset that provides the standard actions
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    renderer_classes = (XLSXRenderer,)
    filename = 'my_export.xlsx'

    @action(detail=True, methods=['post'])
    ...etc...

Have you tried setting the renderer on the parent class rather than within the method?

FlipperPA commented 5 years ago

@lutrasdebtra Any further information?

FlipperPA commented 5 years ago

Closing, no response.