Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use router in Django REST not for viewsets, but for generic views?

I have a very basic question about DjangoREST and routering.

When I need to regist rest_framework.viewsets I am doing somethink like this (example from documentaton):

router = routers.SimpleRouter()
router.register(r'users', UserViewSet)
router.register(r'accounts', AccountViewSet)

But how to add to router views that are from rest_framework.generics package? Should I bild custom router (http://www.django-rest-framework.org/api-guide/routers/#custom-routers)? What is the best practice?

like image 207
pmoniq Avatar asked Jun 06 '16 15:06

pmoniq


People also ask

Should I use Viewsets in django?

Both can be used with normal django urls. Because of the conventions established with the actions, the ViewSet has also the ability to be mapped into a router, which is really helpful. Now, both of this Views, have shortcuts, these shortcuts give you a simple implementation ready to be used.

What is router in Django REST framework?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your index... a resourceful route declares them in a single line of code.

Why Viewsets are used in Django REST framework?

After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet .

What is the difference between View and ViewSet in django?

While regular views act as handlers for HTTP methods, viewsets give you actions, like create or list . The great thing about viewsets is how they make your code consistent and save you from repetition. Every time you write views that should do more than one thing, a viewset is the thing that you want to go for.


1 Answers

But how to add to router views that are from rest_framework.generics package?

You don't. ViewSets adds a couple of compatibility layer to rest_framework.generics in order to work with routers.

Should I bild custom router (http://www.django-rest-framework.org/api-guide/routers/#custom-routers)? What is the best practice?

If you want to use a non viewset view, you'll be down to writing the regular Django url.

My feeling is the real question is quite different and would be something like, "how do I restrict viewset to some actions only".

In which case, the declaration of the ModelViewSet provides the answer:

class ViewSet(ViewSetMixin, views.APIView):
    """
    The base ViewSet class does not provide any actions by default.
    """
    pass


class GenericViewSet(ViewSetMixin, generics.GenericAPIView):
    """
    The GenericViewSet class does not provide any actions by default,
    but does include the base set of generic view behavior, such as
    the `get_object` and `get_queryset` methods.
    """
    pass


class ModelViewSet(mixins.CreateModelMixin,
                   mixins.RetrieveModelMixin,
                   mixins.UpdateModelMixin,
                   mixins.DestroyModelMixin,
                   mixins.ListModelMixin,
                   GenericViewSet):
    """
    A viewset that provides default `create()`, `retrieve()`, `update()`,
    `partial_update()`, `destroy()` and `list()` actions.
    """
    pass

As you can see, you can specialize the ModelViewSet by selecting the required mixins and inheriting from GenericViewSet.

like image 148
Linovia Avatar answered Nov 08 '22 00:11

Linovia