姜戈

其他 社区

技能解读

当前原文的自动摘译,尚未人工审校。

项目结构

遵循标准 Django 应用布局:models.py、views.py、urls.py、admin.py、tests.py、forms.py。让每个应用程序专注于单一领域概念;避免使用不相关模型的“上帝应用程序”。使用settings/base.py、settings/dev.py、settings/prod.py进行特定于环境的配置。

型号

始终在模型上定义__str__,以便管理和调试可读性。谨慎使用 Meta.ordering — 它会将 ORDER BY 添加到每个查询中。在查询集上更喜欢显式的.order_by()。对filter() / order_by()中出现的字段使用数据库索引(db_index=True、Meta.indexes)。对于受约束的字段,优先使用 CharField 和 choices(或 TextChoices / IntegerChoices)而不是裸字符串。

意见

对于 CRUD 更喜欢基于类的视图 (CBV);更喜欢基于函数的一次性逻辑视图。始终显式设置queryset或覆盖get_queryset()——永远不要依赖可变的类级状态。使用select_related()和prefetch_related()可以避免N+1查询问题。设置LOGIN_URL并一致使用@login_required / LoginRequiredMixin。

测试

使用 pytest-django 和 @pytest.mark.django_db 进行数据库访问。仅当需要显式事务控制时才首选TestCase或TransactionTestCase;否则使用 pytest 装置。使用 RequestFactory 或 Client 无需启动服务器即可测试视图。在测试中使用baker.make()(模型面包店)或工厂代替手动模型构建。

陷阱

切勿在异步视图中阻塞 I/O,而不用 sync_to_async 进行包装。避免在 settings.py 或 urls.py 中的模块级别导入模型(循环导入)。永远不要将秘密存储在settings.py中——使用环境变量。避免原始 SQL,除非 ORM 确实无法表达查询。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
通用
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
94 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@microsoft · NOASSERTION
上下文用量评级
低消耗
接入复杂程度
即装即用
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 读取环境变量
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# 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.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。