Separating django business logiv from views

27 views
Skip to first unread message

Mrunalini Ramnath

unread,
Feb 13, 2020, 8:04:23 AM2/13/20
to Django users
Hey folks!
I've been researching on effective ways to separate business logic from views.py. I currently also have separate files for helper functions but the codebase is still a little cluttered. 
I found this method of implementation that involves creating a manager class for each model that can contain all the necessary functions, and thin views for executing said function. There are quite a few sources pertaining to this but I found this to be the strongest one in support so far: https://sunscrapers.com/blog/where-to-put-business-logic-django/#Idea_4_QuerySetsManagers 
Are there any more pros and cons that haven't been discussed in it? 

onlinejudge95

unread,
Feb 13, 2020, 9:12:48 AM2/13/20
to django...@googlegroups.com
On Thu, Feb 13, 2020 at 6:34 PM Mrunalini Ramnath <rmrun...@gmail.com> wrote:
Hey folks!
I've been researching on effective ways to separate business logic from views.py. I currently also have separate files for helper functions but the codebase is still a little cluttered. 

How about going on the traditional MVC way?

Your views act as controllers with only one concern i.e. to operate on the HTTP plane, call services, raise exceptions maybe.

I generally create a service.py in each app to implement the business logic and just call it inside my views.py. It helps to make my business logic loosely coupled with the web-server. Also, it has an upside that it makes testing easier.
 
I found this method of implementation that involves creating a manager class for each model that can contain all the necessary functions, and thin views for executing said function. There are quite a few sources pertaining to this but I found this to be the strongest one in support so far: https://sunscrapers.com/blog/where-to-put-business-logic-django/#Idea_4_QuerySetsManagers 
Are there any more pros and cons that haven't been discussed in it? 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a893a097-c091-42ae-9638-ec4cf0ec8a9d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages