I am trying to create a backend with Django Rest Framework and am trying to determine where to place the business logic. Would it go in the views.py? I would like to create more complex services than just getting a list of objects or grabbing one specific object. Any guidance would be appreciated, thanks. I realize there is a discussion about the business logic in a generic Django project but I am asking specifically about the django rest framework.
The Django REST framework (DRF) is a toolkit built on top of the Django web framework that reduces the amount of code you need to write to create REST interfaces.
Django REST framework is a powerful and flexible toolkit for building Web APIs. Some reasons you might want to use REST framework: The Web browsable API is a huge usability win for your developers. Authentication policies including packages for OAuth1a and OAuth2.
It is more about design patterns rather than Django Rest Framework.
Here are some tips:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With