• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

558
Vistas
generics vs viewset in django rest framework, how to prefer which one to use?

How to prefer which one of generics and viewset to use? in other words when should I use generics and when should I use viewset for building api. I know that they do the same thing, but viewset has routers, so in what situation generics are better than viewset?

over 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

My golden rule for this is to use generics whenever I have to override the default methods to accomplish different specifications from list and details views.

For instance, when you have different serializer classes for listing your resources and for retrieving a resource details by id I consider that using generics is a better option since probably these two endpoints' logic is going to evolve separately. Keep in mind is a good practice to maintain different logics decoupled.

When your endpoint is very simple and you don't need to customize logic between list/create and retrieve/update/delete operations you can use viewset, but yet having in mind it may be good to separate it in two views in case these operations' logic start growing in different paths.

over 3 years ago · Santiago Trujillo Denunciar

0

They are different, let's see.

DRF has two main systems for handling views:

  1. APIView: This provides methods handler for http verbs: get, post, put, patch, and delete.
  2. ViewSet: This is an abstraction over APIView, which provides actions as methods:
    • list: read only, returns multiple resources (http verb: get). Returns a list of dicts.
    • retrieve: read only, single resource (http verb: get, but will expect an id in the url). Returns a single dict.
    • create: creates a new resource (http verb: post)
    • update/partial_update: edits a resource (http verbs: put/patch)
    • destroy: removes a resource (http verb: delete)

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.

GenericAPIView: for APIView, this gives you shortcuts that map closely to your database models. Adds commonly required behavior for standard list and detail views. Gives you some attributes like, the serializer_class, also gives pagination_class, filter_backend, etc

GenericViewSet: There are many GenericViewSet, the most common being ModelViewSet. They inherit from GenericAPIView and have a full implementation of all of the actions: list, retrieve, destroy, updated, etc. Of course, you can also pick some of them, read the docs.

So, to answer your question: DRY, if you are doing something really simple, with a ModelViewSet should be enough, even redefining and calling super also is enough. For more complex cases, you can go for lower level classes.

Hope to have helped you!

over 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda