In my Django app, I am trying to figure out where to include my business logic. The logic does not fit any models(assume it's a one-page app that doesn't have any models) so I want to add a module that holds the logic. For example,
project/app/my_logic.py
def calculate(number_one, number_two):
return number_one + number_two
Then, I would use the logic like,
project/app/views.py
def index(request):
number = my_logic.calculate(1, 2) #can I do this?
return HttpResponse("the number is: %s " % number)
Questions:
Where is the right place to put my_logic.py?
Is it conventional?
What might be a better way?
Note: this is how you import your module (if anyone else is trying to figure out how to do it)
project/app/your_module/your_module.py
project/app/your_module/__init__.py
from views.py,
from app.your_module import your_module
解决方案
Depends, but if it's a logical unit, then a separate python module would be a good start and you would of course write its own unit tests for it (as opposed to Django's built-in integration tests with TestClient).
If it needs to work within the model, then a property on model would be a better place.