# Django Rules

Full coding rules for this stack. Read this in full before writing or modifying any Django code in this project — not just once, keep applying it to every edit in the session, not only the first.

---

## Project Structure

Follow the **MVT (Model-View-Template)** pattern or **MTV** for REST APIs with Django REST Framework (DRF):

```
project/
├── core/            # Project settings, wsgi, asgi
└── apps/
    └── [app-name]/
        ├── models.py      # Database models
        ├── views.py       # API views or Template views
        ├── serializers.py # DRF serializers
        ├── services.py    # Business logic (prefer over logic in views/models)
        ├── urls.py        # App-specific routing
        ├── tests.py       # Tests
        └── admin.py       # Admin configuration
```

---

## Django Rules

- Use **Class-Based Views (CBVs)** for standard REST operations.
- Prefer **Django REST Framework (DRF)** for building APIs.
- Keep business logic in **Services** (or Action classes) rather than in Models or Views to keep them thin.
- Always use **Serializers** for data validation and transformation.
- Leverage Django's built-in **Authentication** and **Permission** systems.

---

## Security Rules

- Use `environ` for sensitive settings (DEBUG, SECRET_KEY).
- Never use `DEBUG = True` in production.
- Always validate input through Forms or Serializers.

---

## Testing Rules

- Use **Django Test Case** or **Pytest-Django**.
- Use `factories` (FactoryBoy) for object creation in tests.
