1

I'm trying to implement some particular views to my ViewSet but keeping a clean and elegant code. I have some servers and some datacenters;

models.py

class Datacenter(models.Model):

    name = models.CharField(max_length=50)
    status = models.CharField(max_length=50)

    def __unicode__(self):
        return self.name

class Server(models.Model):

    datacenter = models.ForeignKey(Datacenter,related_name="servers")
    hostname = models.CharField(max_length=50)

    def __unicode__(self):
        return self.hostname

serializers.py

class ServerSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Server

class DatacenterSerializer(serializers.ModelSerializer):
    Servers = ServerSerializer(many=True)
    class Meta:
        model = Datacenter
        fields = ('id', 'name','status','Servers')

views.py

class DatacenterViewSet(viewsets.ModelViewSet):

    queryset = Datacenter.objects.all()
    serializer_class = DatacenterSerializer

class ServerViewSet(viewsets.ModelViewSet):

    queryset = Server.objects.all()
    serializer_class = ServerSerializer

urls.py

router = routers.DefaultRouter()
router.register(r'Enviroment', views.EnviromentViewSet)
router.register(r'Server', views.ServerViewSet)

urlpatterns = patterns('',url(r'^', include(router.urls)))

So a clear sample of what I would like to implement is a view of all the servers in one datacenter like;

/datacenter/{id}/servers

I check other similar questions like this one and this one but I'm not sure those are actually the best ways, also I was unable to make it work as described in the answer given by mariodev

I'm trying to avoid to solve this using url patterns like Tom Dickin suggested

As far as I understand this can be solved using viewsets, routers or generic-views

Whats the simplest solution? Could someone point me into the right direction?

1
  • Well you can use ad-hoc methods in viewsets Commented Feb 3, 2015 at 11:56

1 Answer 1

1

You can use ad-hoc method

serializers.py

class ServerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Server

class DatacenterSerializer(serializers.ModelSerializer):
    servers = ServerSerializer(many=True)
    class Meta:
        model = Datacenter
        fields = ('id', 'name','status','servers')

views.py

class DatacenterViewSet(viewsets.ModelViewSet):

    queryset = Datacenter.objects.all()
    serializer_class = DatacenterSerializer

    @detail_route(methods=['get'], permission_classes=[permissions.IsAdminUser])
    def servers(self, request, pk=None):
        servers = Server.objects.filter(datacenter__id=pk)
        serializer = ServerSerializer(servers, many=True, context={'request': request})
        return Response(serializer.data)

urls.py

router = routers.DefaultRouter()
router.register(r'Enviroment', views.EnviromentViewSet)
router.register(r'server', views.ServerViewSet)
router.register(r'datacenter', views.DatacenterViewSet)

urlpatterns = patterns('',url(r'^', include(router.urls)))
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but I'm getting; <server: test.server > is not JSON serializable
@MarceloGrebois please try again with my updated code
In case I am implementing both a get and a post using an ad-hoc method, how can I differentiate the two - both get routed to the same function right? Will it be an if/else on request.METHOD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.