![]() ![]() ![]() The object’s detail view is responsible for retrieving, updating or deleting the object or in other words handling the GET, PUT and DELETE method requests to the detail view’s url. In this case the object is a Code Snippet as used in the django rest tutorial but you can imagine the object being anything you want, for example a Post or a Comment. Our example will focus on the simple detail view of an object. In the rest of this post we will show how we can achieve the same result with each one of the four different view styles. By the way, a very useful place to start familiarizing yourself with django class based views is the Classy Class-Based Views and the Classy Django REST Framework for Django REST framework class based views respectively. This doesn’t mean that you can’t customize a class based view to any degree of complexity, but someone must be familiar with how class based views work behind the scenes, for example which methods are called in what order, in order to fully follow the view’s logic. Class based views on the other hand give you the possibility to achieve a typical behaviour without much code, almost with no code at all, as will become obvious in a moment. In a nutshell we can say that there are four ways to write a django rest view, beginning from the standard function based view and moving to different styles of class based views.įunction based views are usually chosen when we want to highly customize the typical view logic since it is easier to follow the sequence of actions they describe. But no matter if you are fan of them or not, you have to admit that they offer you the possibility to do so many things with just a few lines of code. As most things in life class based views come with their own pros and cons and their adoption is a matter of preference. Similarly to django, django rest framework offers you the choice of using class based views. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |