Best practice implementation of business logic

406 views
Skip to first unread message

Pascal Schäfer

unread,
Mar 21, 2023, 7:24:23 PM3/21/23
to Django REST framework
Hello,

I'm currently working on a project using Django or Django Rest Framework to implement an API. Now the question came to me what is the best practice implementation when it comes to business logic? Are fat models, fat views, service layers or something else the best practice?

I did already some research and was reading different articles about it and it seems that a lot of people go the way of a service layer to implement a three tier MVC, but I was also reading that implementing an extra service layer could lead to circumvent automatism that drf would do for you. Is there any advice that you can give?

These articles are some that I read before I asked here:

Especially the last one is completely against a service layer.

Thanks in advance!

Best regards,

Pascal

Tj Johnson

unread,
Mar 24, 2023, 2:23:12 PM3/24/23
to Django REST framework
I find that this becomes less and less of a concern the more you learn the frameworks, you aren't limited to implementing logic in models or views, although that is how I started, and other developers too.  You can also extend and customize serializers, or fields, or validators, or model managers, or permissions, or mixins, or middleware etc. there are also hooks.  It's just a matter of choosing the appropriate location of the business logic.

But I think if the developer doesn't know enough, I'd prefer to see "fat views"

Omar Sarmiento Rolo

unread,
Mar 26, 2023, 10:26:12 AM3/26/23
to django-res...@googlegroups.com
I have been working on this too, in my case, I always make the service's layer, fat view or fat serializer or model for me, they are very difficult to test them. 
For example: unitary test, if you have your business  logic in serializer or model, it could be difficult to test, you have to mock many things.
If you have a service's layer between your view and ORM, if you need to test it, you only need to mock the query, and there are some good packages for it.

 

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/93fe5102-de8f-4929-b517-5301d7d74873n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages