In RecipeViewSet class we also define our queryset and serializer_class.Here we provide our permission classes because we don’t provide UnAuthenticated user can called dangerous method update(),delete() etc and Make sure IsOwner. class RecipeViewSet(viewsets.ModelViewSet): Then set our queryset and serializer_class. Here we define our UserViewSet class which is extend our viewsets.Here we use ModelViewSet because the ModelViewSet class inherits from GenericAPIView and includes implementations for various actions, by mixing in the behavior of the various mixin classes.The actions provided by the ModelViewSet class are. class UserViewSet(viewsets.ModelViewSet): Remember we already create custom permissions which is also imported from. viewsets needs to know what is our queryset,serializers that’s way we import our model class and serializers class.If any permission you want to give your viewsets.you can do it, define permissions which is also import from rest_framework. We work on viewsets so import viewsets from rest_framework. Let make it easy what’s happened our above code. Permission_classes = (permissions.IsAuthenticatedOrReadOnly,Ĭlass IngredientViewSet(viewsets.ModelViewSet): This viewset automatically provides `list`, `create`, `retrieve`,Īdditionally we also provide an extra `highlight` action. permissions import IsOwnerOrReadOnlyĬlass UserViewSet(viewsets.ModelViewSet):Ĭlass RecipeViewSet(viewsets.ModelViewSet): In the recipeapi/views.py add this code: from. ViewSet classes are almost the same thing as View classes, except that they provide operations such as read, or update, and not method handlers such as get or put.Ī ViewSet class is only bound to a set of method handlers at the last moment, when it is instantiated into a set of views, typically by using a Router class which handles the complexities of defining the URL conf for you.To more about Know( viewsets). REST framework includes an abstraction for dealing with ViewSets, that allows the developer to concentrate on modeling the state and interactions of the API, and leave the URL construction to be handled automatically, based on common conventions. Some Information about Viewsets & routers:
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |