Django Project

Language: Python October 1, 2025

Instruction file for Django projects with full-stack web development.

---
applyTo: "**/*.py, **/models.py, **/views.py, **/urls.py"
---

# Django Development Instructions (2025)

_Last updated: October 2025_

- Always use the fetch tool to look up the latest Django documentation: https://docs.djangoproject.com
- Use Django 5.0+ for new projects
- Follow Django's MTV (Model-Template-View) architecture
- Leverage Django ORM for database operations
- Use Django's built-in features (admin, auth, forms)
- Follow Django coding style and best practices

**Core Principles:**

- Use Django ORM models for database schema
- Implement class-based views (CBVs) for complex logic
- Use function-based views (FBVs) for simple endpoints
- Leverage Django's built-in authentication system
- Use migrations for all database changes
- Follow the DRY (Don't Repeat Yourself) principle

**Project Structure:**

- Organize apps by feature or domain
- Keep models in models.py, views in views.py
- Use serializers.py for Django REST Framework
- Implement services/ for business logic
- Use management/commands/ for custom commands

**Models:**

- Define models with proper field types and constraints
- Use related_name for reverse relationships
- Implement __str__() for readable representations
- Use Meta class for ordering and indexes
- Create custom managers for complex queries
- Use abstract base classes for shared fields

**Views:**

- Use class-based views (ListView, DetailView, CreateView)
- Implement mixins for reusable view logic
- Use function-based views for simple logic
- Implement proper permission checking
- Use get_queryset() for filtering

**Django REST Framework:**

- Use DRF for building REST APIs
- Implement ModelSerializer for CRUD operations
- Use ViewSets for standard REST endpoints
- Implement custom actions with @action decorator
- Use proper authentication and permissions
- Implement pagination and filtering

**Forms:**

- Use ModelForm for model-based forms
- Implement custom validation with clean methods
- Use form mixins for reusable logic
- Handle form errors properly
- Use crispy-forms for Bootstrap styling

**Admin:**

- Customize Django admin with ModelAdmin
- Use list_display, list_filter, search_fields
- Implement custom admin actions
- Use inline models for related objects
- Customize admin templates when needed

**Authentication:**

- Use Django's built-in User model or extend it
- Implement custom authentication backends if needed
- Use decorators for view protection (@login_required)
- Implement proper password reset flows
- Use django-allauth for social authentication

**Performance:**

- Use select_related() and prefetch_related() to optimize queries
- Implement database indexing
- Use caching (Redis, Memcached)
- Enable QuerySet query optimization
- Use Django Debug Toolbar for profiling

**Testing:**

- Use Django's TestCase for testing
- Implement factory_boy for test fixtures
- Test models, views, and forms separately
- Use pytest-django for advanced testing
- Implement integration tests

**Summary:**

> For all Django work, always use the fetch tool to look up the latest documentation from https://docs.djangoproject.com. Build robust, secure, full-stack web applications.

Django Python Backend Framework Full-stack