A copilot-instructions.md template for Django projects covering models, views, DRF serializers, and testing patterns.
## copilot-instructions.md for Django Projects This is a Django 5.1 project with Django REST Framework. Follow these conventions: ### Project Structure - Each Django app is a self-contained module in `/apps/` directory - App structure: `models.py`, `views.py`, `serializers.py`, `urls.py`, `admin.py`, `tests/` - Shared utilities in `/core/` app: base models, permissions, pagination - Settings split: `base.py`, `development.py`, `production.py` in `/config/settings/` ### Models - Abstract base model with `id` (UUID), `created_at`, `updated_at` fields - Use `models.TextChoices` for choice fields, not raw strings or integers - Add `class Meta: ordering`, `verbose_name`, `verbose_name_plural` - Custom managers for common query patterns: `objects.active()`, `objects.published()` - Use `F()` and `Q()` objects for complex queries; avoid raw SQL - Always add `db_index=True` on frequently filtered fields ### Views (DRF) - ViewSets for standard CRUD, `@api_view` for simple endpoints - Permission classes per view: `IsAuthenticated`, `IsOwnerOrReadOnly` - Use `select_related` and `prefetch_related` to avoid N+1 queries - Return paginated responses for list endpoints - Custom exception handler returning consistent error format ### Serializers - Separate Read and Write serializers when shapes differ - Use `serializers.SerializerMethodField` for computed fields - Validate at serializer level with `validate_<field>` and `validate()` methods - Nested serializers for related objects in read serializers - `SlugRelatedField` or `PrimaryKeyRelatedField` for write serializers ### Authentication - JWT via `djangorestframework-simplejwt` - Custom user model extending `AbstractUser` in `/apps/users/` - Token refresh endpoint at `/api/v1/auth/token/refresh/` - Rate limit login endpoint: 5 attempts per minute ### Testing - `pytest-django` with `conftest.py` fixtures - Factory Boy factories for test data: `UserFactory`, `PostFactory` - API tests use `APIClient` with authenticated fixtures - Test naming: `test_should_[result]_when_[condition]` - Cover edge cases: unauthorized, not found, validation errors ### Migrations - One migration per logical change; squash when migrations pile up - Always review auto-generated migrations before committing - Data migrations in separate files from schema migrations - Never edit already-applied migrations in production
Free to copy and use. Compatible with GitHub Copilot, GPT-5.
Save as .github/copilot-instructions.md in your Django repository. Customize the Django and DRF versions to match your project.
Initial release
Sign in and download this prompt to leave a review.