38143

Django: separating business logic from view logic

Question:

possible refenrece: <a href="https://stackoverflow.com/questions/12578908/separation-of-business-logic-and-data-access-in-django" rel="nofollow">Separation of business logic and data access in django</a>

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:

<ol><li>Where is the right place to put my_logic.py?</li> <li>Is it conventional?</li> <li>What might be a better way?</li> </ol>

<em>Note: this is how you import your module (if anyone else is trying to figure out how to do it)</em>

project/app/your_module/your_module.py project/app/your_module/__init__.py

from views.py,

from app.your_module import your_module

Answer1:

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.

Recommend

  • Reverse {% url %}: Reverse for '\"' not found. '' is not a valid view func
  • Can we print the command line arguments in setup_module() or setup_class in pytest?
  • Django 1.7 error NoReverseMatch at '/post/'
  • 2 forms, 1 view, 2 SQL tables in Django
  • How to get or calculate size of Azure File/Share or Service
  • How to model a mixture of finite components from different parametric families with JAGS?
  • NSMutableArray Access Issue
  • Timing loops with asynchronous functions
  • SQL Server re-calculate or not?
  • Receive mouse move even cursor is outside control
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • Does Apportable support to build library binary (.a/.so)?
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • How can I speed up CURL tasks?
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Bad request using file_get_contents for PUT request in PHP
  • Typescript - Unable to get 'import' statement to function
  • Switching to Release Build causes runtime error in Web Reference
  • Meteor helpers not available in Angular template
  • Seeking advice on Jetty HttpClient Hang
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • How would I use PHP exceptions to define a redirect?
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Symfony2: How to get request parameter
  • Rearranging Cells in UITableView Bug & Saving Changes
  • QuartzCore.framework for Mono Develop
  • Traverse Array and Display in markup
  • log4net write single file for each call to log.info
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Getting error when using KSoap library to consume .NET web services
  • using HTMLImports.whenReady not working in chrome
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?