Giter Site home page Giter Site logo

Comments (4)

johanvdw avatar johanvdw commented on May 16, 2024

I'm also interested in this - or just a way to route extra actions when having a pandasviewset.
http://www.django-rest-framework.org/api-guide/routers/#routing-for-extra-actions

Eg what I'd like to do:

/api/buildings
/api/buildings/1/doors --> give a csv with all doors for a specific building

If I use a ModelViewSet (standard) this will work (returning json) when using

@action(detail=True)
def doors(self, request, pk):

inside the viewset. If I change the viewset to a PandasViewset, it seems these actions no longer work.

from django-rest-pandas.

johanvdw avatar johanvdw commented on May 16, 2024

Answering myself :

You can achieve this by using nested routing, with the drf-nested-routing package.

Register a nested router:

# urls.py
from rest_framework_nested import routers
building_router = routers.NestedSimpleRouter(building, 'building', lookup='building')
building_router.register('doors', views.DoorsViewset, base_name='building-doors')

And add a view:

class DoorsViewset(PandasViewSet):
    def get_queryset(self):
        print (self.kwargs)
        queryset = Doors.objects.filter(cable_id=self.kwargs['building_pk'])
        return queryset
    serializer_class = DoorsSerializer

from django-rest-pandas.

sheppard avatar sheppard commented on May 16, 2024

Since most of the work happens in the serializer and renderers, you can also do this without PandasView/PandasViewSet. One option would be to override get_renderers() and explicitly set list_serializer_class, something like this:

class DoorSerializer(ModelSerializer):
    class Meta:
        list_serializer_class = PandasSerializer

class BuildingViewSet(ModelViewSet):
    def get_renderers(self):
        if self.action == 'doors':
             return [PandasCSVRenderer(), ...]
        else:
            return super().get_renderers()

    @action(detail=True)
    def doors(self, request, pk):
        queryset = Doors.objects.filter(...)
        return DoorSerializer(queryset, many=True).data

from django-rest-pandas.

sheppard avatar sheppard commented on May 16, 2024

I added some more information to the README.

from django-rest-pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.