To start, you may want to look into overriding the get_queryset method which is defined on ViewSets, that way you can restrict the query set that all of the default methods use. As far as handling creation goes (properly associating it), you may want to look into the creation hooks that DRF provides.
Kevin Brown On Sun, Nov 27, 2016, 15:21 Quentin Stafford-Fraser <[email protected]> wrote: > Hi there - > > I currently need a fairly simple API except for the fact that I'd like to > host multiple sites on one server (using a variation on the Django Sites > framework). > > When you get a list of objects, it should only be the ones in that Site > (taken from the host part of the URL). When you create one, it should be > created in the right Site, etc. > > Anyone recommend the neatest way of doing this? I've started to override > each of the methods on the viewset for my 'Location' class, but thought > there might be a Right Way... :-) > > Any recommendations much appreciated, before I get too stuck in... > Quentin > > class LocationViewSet(viewsets.ModelViewSet): > ... > def list(self, request, format=None): > queryset = Location.objects.filter(site=request.site) > ser_class = LocationSerializer > serializer = ser_class(queryset, many=True, context={'request': > request}) > return Response(serializer.data) > > etc. > > -- > You received this message because you are subscribed to the Google Groups > "Django REST framework" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
