0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Ya I guess in hindsight I maybe would have implemented them. Function based views just felt like the natural progression and worked for the project I was working on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.