I am new to Django Rest Framework. Using serializer
and views
a simple CRUD is easy. When the logics increase, it is quite confusing where to write logics in serializer
or views
.
Some developers do prefer "Thick serializer and thin views" and some developers "Thick views and thin serializer".
Maybe this is not a big issue and I think it is up to the developer whether to write more on views
or serializer
, but as a newbie what will be your suggestion to follow? Should I write more on views
or serializer
?
There are many answers on Django View Template but can not find a satisfying answer for Django Rest Framework.
Thoughts of experienced developers will be highly appreciated. Thank you.
相关问题
- Django __str__ returned non-string (type NoneType)
- Design RESTful service with multiple ids
- Django & Amazon SES SMTP. Cannot send email
- Json.NET deserializing contents of a JObject?
- Axios OPTIONS instead of POST Request. Express Res
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- serializing a list of objects into a file in java
- Django/Heroku: FATAL: too many connections for rol
- Convert C# Object to Json Object
- Serialise choice text for IntegerField with choice
- When sending XML to JMS should I use TextMessage o
I've asked a similar question about this before. Actually, it depends on what logic you are going to adapt. After doing further research, I have come up with some approaches. Here are my suggestions:
create
method). An example to this case would be; your POST body does not contain a value that is needed by the serializer hence is not valid yet, so add that in your view'screate
function.SerializerMethodField
etc. to do that.Personally I prefer to have the business logic separated from both view and serializer. I usually create a new class with the business logic which can be used in both serializer and view based on necessity. Basically I treat it as a service. Reason for that is:
An example would be like this:
Example usage in serializer:
I try to segregate it on the basis of requirements of the context. Like:
request
object(like current user), I try to implement that inview
.serializer
.It may also depend upon how I'm planning to maintain the code (If I have huge number of views in a single file, I will try to avoid implementing logical stuffs as much as possible).