dj-services

Refactor Django business logic into service classes with svcs dependency injection.

109|6|Updated Apr 4, 2026
One-click install
npx skills add https://github.com/dvf/opinionated-django --skill dj-services
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dj-services
Source: https://github.com/dvf/opinionated-django/tree/main/skills/dj-services
Command: npx skills add https://github.com/dvf/opinionated-django --skill dj-services

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill solves the common Django pain point of tangled, hard-to-test code where business logic is mixed into views, model methods, or scattered across the codebase, making changes risky and tests slow and dependent on the database.

Core Features & Use Cases

  • Structured Service Layer: Enforces plain Python service classes that hold all business logic, with dependencies injected via constructor for full testability.
  • Centralized Dependency Injection: Uses the svcs registry to wire services and repositories once at startup, so they can be resolved anywhere — views, Celery tasks, management commands, and tests.
  • Strict Separation of Concerns: Services never import Django ORM or models, all database access is delegated to repositories, and all layer boundaries use typed Pydantic DTOs to keep changes local.
  • Use Case: Refactor a fat Django view that handles order creation, inventory checks, and payment processing into a dedicated OrderService, inject the required OrderRepository and ProductRepository, register it in the svcs registry, and write fast, database-free unit tests for the business logic.

Quick Start

Use the dj-services skill to refactor the order creation logic from your Django view into a dedicated OrderService class with injected repository dependencies, registered in the project's svcs registry.

Frequently Asked Questions about dj-services

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I separate Django business logic from ORM and framework concerns?

To separate Django business logic from ORM concerns, extract logic into plain Python service classes that delegate all database access to repositories and communicate across boundaries using typed Pydantic DTOs. This ensures services never import Django models directly.

How do I refactor a fat Django view into a testable service layer?

Refactor a fat Django view into a testable service layer by moving order creation, inventory checks, and payment processing into a dedicated stateless service class with constructor-injected repository dependencies, then register it in a centralized DI registry.

How do I write database-free unit tests for Django business logic?

Write database-free unit tests for Django business logic by using constructor-injected dependencies in stateless service classes so you can inject mock repositories directly, eliminating slow database queries and isolating pure business rules.

What is the svcs dependency injection registry used for in Django projects?

The svcs dependency injection registry is used to wire Django services and repositories once at startup so they can be resolved anywhere across views, Celery tasks, management commands, and tests without tight coupling or direct ORM imports.

When should I use the repository pattern and service layer in Django?

Use the repository pattern and service layer in Django when business logic is tangled across views and model methods, making changes risky and tests slow. This architecture enforces strict separation of concerns and enables fast, isolated testing.

Can I resolve registered Django services inside Celery tasks and management commands?

Yes, you can resolve registered Django services inside Celery tasks and management commands. The centralized svcs registry configures dependency injection at startup, allowing services to be consistently resolved across views, background tasks, and commands.