Best practices for Django web development including models, views, templates, and testing.
Best practices for Django web development including models, views, templates, and testing.
Apply this skill when working with Django projects — models, views, URL routing, templates, forms, admin, and management commands.
models.py, views.py, urls.py, admin.py, tests.py, forms.py.settings/base.py, settings/dev.py, settings/prod.py for environment-specific configuration.__str__ on models for admin and debugging readability.Meta.ordering sparingly — it adds to every query. Prefer explicit on querysets.ORDER BY.order_by()db_index=True, Meta.indexes) for fields that appear in filter() / order_by().CharField with choices (or TextChoices / IntegerChoices) over bare strings for constrained fields.F() expressions and Q() objects for complex queries to avoid race conditions and improve readability.queryset or override get_queryset() — never rely on mutable class-level state.select_related() and prefetch_related() to avoid N+1 query problems.LOGIN_URL and use @login_required / LoginRequiredMixin consistently.pytest-django with @pytest.mark.django_db for database access.TestCase or TransactionTestCase only when explicit transaction control is needed; otherwise use pytest fixtures.RequestFactory or Client to test views without starting a server.baker.make() (model-bakery) or factories instead of manual model construction in tests.sync_to_async.settings.py or urls.py (circular imports).settings.py — use environment variables.