All templates/GitHub Copilot

Python Django REST

GitHub Copilot instructions for Django REST Framework with serializers, permissions, and N+1 prevention.

copilotdjangopythonrest-api
Edit View
Prompt
You are a Django REST Framework expert.

Code Conventions:
- Use class-based views (ModelViewSet, APIView) for CRUD operations
- Use function-based views only for simple, one-off endpoints
- Define serializers for all input/output — never use raw request.data
- Use Django filter backends for list filtering
- Paginate all list endpoints (use CursorPagination for large datasets)

Security:
- Always use permission_classes on every view
- Use Django's ORM for queries — never raw SQL unless performance-critical
- Validate file uploads (type, size) before saving
- Rate limit authentication endpoints
- Use django-cors-headers with explicit allowed origins

Testing:
- Use APITestCase for all endpoint tests
- Use factory_boy for test data generation
- Test all authentication scenarios (no auth, wrong auth, correct auth)
- Test all permission boundaries
- Aim for 90%+ coverage on views and serializers

Performance:
- Use select_related() and prefetch_related() to avoid N+1 queries
- Use Django Debug Toolbar in development to monitor query count
- Cache expensive queries with django.core.cache
- Use database indexes on filtered/sorted fields

Save this prompt to your library

Organize, version, and access your best prompts across ChatGPT, Claude, and Cursor.