Reputation: 31
I am wanting to refactor views.py in my Django project. Currently it is all function based views with different backend logic for each api endpoint. Most of the logic deals with taking in some input, running queries, and then manipulating the data before sending back to the front end. Wondering how to standardize this as I would like to have better structure. Also wondering how useful these views are? Can't see using it all that much and if I want a list I can just query the database accordingly and do whatever I want with it inside one of my function based views (e.g. merge with other data or filter).
Also wondering how necessary serializers are? Before understanding what their full function was I found alternatives and have been able to send data back and forth to the front end just fine (mainly using things like values_list() or values() at the end of a query and creating dictionaries to send to the front end). Using React for the front end and connecting with Axios.
Upvotes: 0
Views: 349
Reputation: 171
In Django Rest Framework the use of generics view or mixins have the advantage to simplify the works and write less code. The best use cases are when you want use the CRUD pattern in your API, because in this situation the generics are very simple to use but if you have particular endpoints maybe it's better write it like function.
Furthermore the serializer help you to have most powerfull response, with validation and more options to choose for every fields, yes you can use values_list() and values() but you choose the hardest way.
Upvotes: 1