Structuring Large Django Applications
Organize a growing Django codebase around business capabilities, explicit services, focused selectors, and useful boundaries without wrapping every ORM operation.
A large Django application rarely becomes difficult because models.py has too many lines. It becomes difficult when a checkout view changes inventory, billing code imports private order helpers, and background tasks duplicate the same business rules. Moving those functions into more folders does not repair the dependencies.

Start with business ownership. Orders owns order state transitions. Billing owns invoices and payment records. Inventory owns stock reservations. A Django app is a feature package wired into the project; services and selectors are optional design conventions you add when they clarify responsibilities.
Django projects and applications
Step 1: Use Capability-Oriented Apps
# Example project layout; each business app owns its migrations.
# orders/
# models.py
# services.py write workflows
# selectors.py read queries
# api.py HTTP adaptation
# tasks.py background entry points
# tests/
# billing/
# inventory/Avoid apps named services, serializers, or utils as the primary organizational boundary. Those describe technical categories that grow across every business capability. A small shared module is fine for stable primitives, but do not turn it into the place where ownership questions disappear.
Step 2: Give Write Workflows an Explicit Entry Point
# orders/services.py -- illustrative application contract
from django.db import transaction
from orders.models import Order
from inventory.services import reserve_items
@transaction.atomic
def submit_order(*, tenant_id, actor, order_id):
order = Order.objects.select_for_update().get(
tenant_id=tenant_id, pk=order_id
)
order.assert_submittable_by(actor)
reserve_items(tenant_id=tenant_id, order=order)
order.status = Order.Status.SUBMITTED
order.save(update_fields=['status'])
return orderThe model method and inventory service are project-specific contracts. Define who checks authorization, which transaction contains the operation, and which state transitions are permitted. This example assumes both apps use the same database; a local atomic block does not coordinate transactions across separate services or databases.
Step 3: Name Read Shapes with Selectors
# orders/selectors.py
def order_list(*, tenant_id):
return (
Order.objects.filter(tenant_id=tenant_id)
.select_related('customer')
.order_by('-created_at', '-id')
)A selector makes the response’s filtering and loading requirements discoverable. It can return a queryset when callers need pagination, or a projection when the read has a fixed shape. Do not create a selector that merely renames objects.get unless it captures a meaningful policy.
Where Repositories Earn Their Cost
The Django ORM already provides a rich persistence interface. Wrapping every filter, update, and get in a generic repository often removes useful expressiveness while adding maintenance. A repository becomes valuable when domain code must operate against a narrower storage contract, when multiple persistence implementations are real requirements, or when an external system owns the data.
Keep the abstraction specific to the capability: load_open_invoice or save_reservation expresses more than GenericRepository.update. Fake implementations should reproduce the behavior that callers rely on, including conflicts and missing records, rather than returning success unconditionally.
Keep Dependency Direction Visible
Allow cross-app calls through a small public service surface. If Orders and Billing repeatedly import each other, move the orchestration into a checkout workflow or revisit which app owns that process. Imports inside functions can defer a circular import error, but they do not resolve the architectural cycle.
Test pure rules without the database, selectors against realistic relationships, and service workflows with persistence. Add a cross-app integration test for the business path that would hurt most if it broke. Split files only when navigation improves; preserve the ownership boundary as the codebase grows.