Django Business Logic A Comprehensive Guide to Services and Managers - zamaniamin/python GitHub Wiki
Both services and managers are used to encapsulate business logic in Django applications. However, they serve different purposes and have different strengths and weaknesses.
Services
Services are typically used for more complex business logic that involves multiple models or interacts with external systems. They are often responsible for orchestrating the flow of data between different parts of the application and making sure that everything happens in the right order. Services can also be used to encapsulate complex logic that would be difficult or inefficient to write directly in a view or template.
Managers
Managers are typically used for simpler business logic that is related to a particular model. They are often responsible for creating, retrieving, updating, and deleting objects from the database. Managers can also be used to perform validation checks and apply business rules to the data.
When to use services?
Use services when:
- You need to encapsulate complex business logic that involves multiple models or interacts with external systems.
- You need to orchestrate the flow of data between different parts of the application.
- You need to encapsulate difficult or inefficient logic that would be difficult or inefficient to write directly in a view or template.
When to use managers?
Use managers when:
- You need to perform simple CRUD (Create, Retrieve, Update, Delete) operations on a model.
- You need to validate data before it is saved to the database.
- You need to apply business rules to the data.
In general, it is a good practice to use services for more complex business logic and managers for simpler business logic. This will help to keep your code organized and maintainable.
Here is an example of how to use a service to encapsulate complex business logic:
from django.core.exceptions import ValidationError
from django.db import transaction
from myapp.models import Product, Order
class OrderService:
def create_order(self, customer_id, order_items):
order = Order.objects.create(customer_id=customer_id)
for item in order_items:
product = Product.objects.get(pk=item['product_id'])
order_item = order.items.create(
product=product, quantity=item['quantity'])
if order_item.quantity > product.stock:
raise ValidationError('Not enough stock')
product.stock -= order_item.quantity
product.save()
return order
Here is an example of how to use a manager to encapsulate simple CRUD operations:
from django.db import transaction
from myapp.models import Product
class ProductManager(models.Manager):
def create_product(self, name, price):
product = Product(name=name, price=price)
product.save()
return product
def update_product(self, product_id, new_name, new_price):
product = Product.objects.get(pk=product_id)
product.name = new_name
product.price = new_price
product.save()
return product
def delete_product(self, product_id):
product = Product.objects.get(pk=product_id)
product.delete()