where exactly should python business logic be placed in django

I have just begun to learn Django/Python/Web Development. This problem has been troubling me for a while now.

I am creating an application with multiple templates in Django. I have a views.py which is basically just rendering the responses to the respective templates and I have a models.py where I have structured my DB. In one of my templates, I need to upload an image (which I am able to do) and I need to run a logic which is based on the features of the uploaded image (not yet done). This logic involves a lot of heavy calculations. After performing the calculations, the logic should return some processed information (coordinates) to the template.

I have been able to do all this actions successfully in a standalone python desktop application calling python files one after the other. However, since I now want to make this a web application I have begun using the Django framework.

I have done a lot of searching but I am still not able to figure out where exactly should I place this Python file containing all the logic. Should I have another class based file (logic.py) and call it from the view.py? I googled and found that many developers are placing their business logic in their models.py in Django. However, I feel it is intuitively not right since model should exclusively communicate with the back end. Any help would be appreciated.Thanks in advance.

2

I have done a lot of searching but I am still not able to figure out where exactly should I place this Python file containing all the logic.

There are a number of options, depending on what your requirements are:

  1. Add the logic to e.g. the Image model. This is a useful option if you need to store per-image meta data in the database, and each model instance (each image) is processed by itself.

  2. Add the logic as a plain Python Image class, e.g. in a file called image.py. Nothing in Django restricts you from adding logic other than that in the views or models modules. This is a good option if the image logic is a central component of your Django app (e.g. a Image processing app).

  3. Create a separate Python project that provides the logic, then call it from your views. Make sure to install this project in your Django app’s Python environment. This option is valid if the purpose of your Django app is to upload and view images, or to show the results of the image processing in direct response to a user’s request, but where the image processing could be used by other projects too.

  4. Create a separate app that processes requests asynchronously and is run separately from your Django app. This option is useful if you need to decouple the image processing from the request cycle of the app, process large number of images, or where each calculation takes too much time to solve within a request cycle’s time (say within at most 500ms to 1s).

I feel it is intuitively not right since model should exclusively communicate with the back end.

There is nothing in Django that requires a model to communicate with the back end, or rather the database. I think you are mixing the semantics of what Django typically considers a model (namely, an abstraction of one or several tables in the database), v.s. the term model as a design construct (e.g. as in Domain Driven Design).

2

Daniel Greenfeld, co-author of “Two Scoops of Django, recommends the business logic should be in the models “when possible, or in the forms if you must.” As for Bart’s possible duplicate, django may be similar to MVC but it is not MVC. As explained here in the official django documentation faq. @adrita, I think you need may need to go review the official documentation to help you understand the concept of models, views, and templates a bit better.

2

In the official Django docs https://docs.djangoproject.com/en/1.11/, it says:

Django has the concept of “views” to encapsulate the logic responsible
for processing a user’s request and for returning the response. Find
all you need to know about views via the links below:

Django recommends the logic to be contained within the views.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật