

They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself. were developed as a shortcut for common usage patterns. For example: class AccountViewSet(viewsets.Django’s generic views. Exampleīecause ModelViewSet extends GenericAPIView, you'll normally need to provide at least the queryset and serializer_class attributes. The actions provided by the ModelViewSet class are. The ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes. In order to use a GenericViewSet class you'll override the class and either mixin the required mixin classes, or define the action implementations explicitly. The GenericViewSet class inherits from GenericAPIView, and provides the default set of get_object, get_queryset methods and other generic view base behavior, but does not include any actions by default. In order to use a ViewSet class you'll override the class and define the action implementations explicitly. The ViewSet class does not provide any implementations of actions. You can use any of the standard attributes such as permission_classes, authentication_classes in order to control the API policy on the viewset. Additionally, this method can be used to reverse the default actions, such as list and create. reverse_action() should match the same argument to the decorator. > view.reverse_action(t_password.url_name, args=) ' Alternatively, you can use the url_name attribute set by the decorator. Using the example from the previous section: > view.reverse_action('set-password', args=) If you are not using a router, then you must provide the basename argument to the. Note that the basename is provided by the router during ViewSet registration. This is a convenience wrapper for reverse(), automatically passing the view's request object and prepending the url_name with the. If you need to get the URL of an action, use the. """Update the user's delete_password(self, request, pk=None): Note that additional mappings do not accept arguments.
#Django rest framework mixins password#
For example, the above password set/unset methods could be consolidated into a single route. Routing additional HTTP methods for extra actionsĮxtra actions can map additional HTTP methods to separate ViewSet methods. Use the url_path and url_name parameters to change the URL segment and the reverse URL name of the action. If we need to, we can bind this viewset into two separate views, like so: user_list = UserViewSet.as_view(/unset_password/$.


User = get_object_or_404(queryset, pk=pk) Serializer = UserSerializer(queryset, many=True) from import Userįrom django.shortcuts import get_object_or_404įrom rializers import UserSerializerįrom rest_framework.response import ResponseĪ simple ViewSet for listing or retrieving users. Let's define a simple viewset that can be used to list or retrieve all the users in the system. Typically, rather than explicitly registering the views in a viewset in the urlconf, you'll register the viewset with a router class, that automatically determines the urlconf for you. The method handlers for a ViewSet are only bound to the corresponding actions at the point of finalizing the view, using the. post(), and instead provides actions such as. In other frameworks you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'.Ī ViewSet class is simply a type of class-based View, that does not provide any method handlers such as. 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.ĭjango REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet.
