Hosting multiple Sites via one API

21 views
Skip to first unread message

Quentin Stafford-Fraser

unread,
Nov 27, 2016, 3:21:57 PM11/27/16
to Django REST framework
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.

Kevin Brown

unread,
Nov 27, 2016, 6:10:52 PM11/27/16
to Django REST framework

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


--
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 django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Quentin Stafford-Fraser

unread,
Nov 27, 2016, 6:50:23 PM11/27/16
to Django REST framework
Ah - many thanks! And searching the docs, I realise that I would have discovered that if I'd read just a bit further.

Need to read more before starting implementation. It's nice to use a system where having too much good documentation is the problem :-)

Much obliged,
Q

Reply all
Reply to author
Forward
0 new messages