简介
Django
原始描述
Builds, debugs, and hardens Django apps: models, the ORM, views, templates, forms, the admin, DRF APIs, and deployment. Use when a page fires one query per row (N+1, select_related, prefetch_related, annotate double-counting); when makemigrations conflicts, a migration locks a live table, or InconsistentMigrationHistory blocks a deploy; on 403 CSRF verification failed, DEBUG=False turning every request into 400 DisallowedHost or a blank 500, or SECURE_SSL_REDIRECT looping behind a proxy; on SynchronousOnlyOperation, AppRegistryNotReady, or NoReverseMatch; when a background task runs before its transaction commits, signals fire on rows that roll back, or update() skips auto_now; when the admin times out on a big table, collectstatic breaks static files, or workers exhaust database connections; when writing serializers, formsets, a custom user model, permissions, or assertNumQueries tests; or upgrading Django across a deprecation. Not for plain Python, FastAPI or Flask services, or engine-level SQL tuning.
通过你的 Agent 添加
安装目录:django/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
name: django
description: Best practices for Django web development including models, views, templates, and testing.
Skill: Django
Best practices for Django web development including models, views, templates, and testing.
When to Use
Apply this skill when working with Django projects — models, views, URL routing, templates, forms, admin, and management commands.
Project Structure
- Follow the standard Django app layout:
models.py, views.py, urls.py, admin.py, tests.py, forms.py.
- Keep each app focused on a single domain concept; avoid "god apps" with unrelated models.
- Use
settings/base.py, settings/dev.py, settings/prod.py for environment-specific configuration.
Models
- Always define
__str__ on models for admin and debugging readability.
- Use
Meta.ordering sparingly — it adds ORDER BY to every query. Prefer explicit .order_by() on querysets.
- Use database indexes (
db_index=True, Meta.indexes) for fields that appear in filter() / order_by().
- Prefer
CharField with choices (or TextChoices / IntegerChoices) over bare strings for constrained fields.
- Use
F() expressions and Q() objects for complex queries to avoid race conditions and improve readability.
Views
- Prefer class-based views (CBVs) for CRUD; prefer function-based views for one-off logic.
- Always explicitly set
queryset or override get_queryset() — never rely on mutable class-level state.
- Use
select_related() and prefetch_related() to avoid N+1 query problems.
- Set
LOGIN_URL and use @login_required / LoginRequiredMixin consistently.
Testing
- Use
pytest-django with @pytest.mark.django_db for database access.
- Prefer
TestCase or TransactionTestCase only when explicit transaction control is needed; otherwise use pytest fixtures.
- Use
RequestFactory or Client to test views without starting a server.
- Use
baker.make() (model-bakery) or factories instead of manual model construction in tests.
Pitfalls
- Never do blocking I/O in async views without wrapping in
sync_to_async.
- Avoid importing models at module level in
settings.py or urls.py (circular imports).
- Never store secrets in
settings.py — use environment variables.
- Avoid raw SQL unless the ORM genuinely cannot express the query.
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.- Always define
__str__on models for admin and debugging readability. - Use
Meta.orderingsparingly — it addsORDER BYto every query. Prefer explicit.order_by()on querysets. - Use database indexes (
db_index=True,Meta.indexes) for fields that appear infilter()/order_by(). - Prefer
CharFieldwithchoices(orTextChoices/IntegerChoices) over bare strings for constrained fields. - Use
F()expressions andQ()objects for complex queries to avoid race conditions and improve readability.
Views
- Prefer class-based views (CBVs) for CRUD; prefer function-based views for one-off logic.
- Always explicitly set
queryset or override get_queryset() — never rely on mutable class-level state.
- Use
select_related() and prefetch_related() to avoid N+1 query problems.
- Set
LOGIN_URL and use @login_required / LoginRequiredMixin consistently.
Testing
- Use
pytest-django with @pytest.mark.django_db for database access.
- Prefer
TestCase or TransactionTestCase only when explicit transaction control is needed; otherwise use pytest fixtures.
- Use
RequestFactory or Client to test views without starting a server.
- Use
baker.make() (model-bakery) or factories instead of manual model construction in tests.
Pitfalls
- Never do blocking I/O in async views without wrapping in
sync_to_async.
- Avoid importing models at module level in
settings.py or urls.py (circular imports).
- Never store secrets in
settings.py — use environment variables.
- Avoid raw SQL unless the ORM genuinely cannot express the query.
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.- Use
pytest-djangowith@pytest.mark.django_dbfor database access. - Prefer
TestCaseorTransactionTestCaseonly when explicit transaction control is needed; otherwise use pytest fixtures. - Use
RequestFactoryorClientto test views without starting a server. - Use
baker.make()(model-bakery) or factories instead of manual model construction in tests.
Pitfalls
- Never do blocking I/O in async views without wrapping in
sync_to_async.
- Avoid importing models at module level in
settings.py or urls.py (circular imports).
- Never store secrets in
settings.py — use environment variables.
- Avoid raw SQL unless the ORM genuinely cannot express the query.
sync_to_async.settings.py or urls.py (circular imports).settings.py — use environment variables.