It is more about design patterns rather than Django Rest Framework.
Here are some tips:
- Providing interfaces using REST should not involve any specific code related to data manipulation or business logic.
- Using an MVC approach does not mean that you shouldn’t layer your application.
- You should be able to test your business logic without touching the UI at all.
- Some people may suggest putting business logic in models. But I do not agree with them, since Django models are different from domain models and business related tasks such as tax calculation.
- Before getting stuck in MVC, You could read more about The MVC implemented in MVC three-tier architecture
- I suggest having a business layer and related apps putting your business logic there.
Suppose that you have an online coffee shop & you’d like to provide a REST API for ordering coffees.
Here are my suggested code samples:
myapp/views.py:
def order(request, quantity=1):
# Process the order by calling the mapped method
order_id = CoffeeShopService.place_order(quantity)
return HttpResponse({'order_id': order_id, mimetype="application/json")
myapp/services.py:
class CoffeeShopService(object):
@staticmethod
def place_order(quantity):
# do the business logic here
return order_id