“Django/docs/2.2.x/ref/django-admin”的版本间差异

来自菜鸟教程
Django/docs/2.2.x/ref/django-admin
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:django-admin 和 manage.py — Django 文档}}
 
<div id="django-admin-and-manage-py" class="section">
 
<div id="django-admin-and-manage-py" class="section">
  
= <code>django-admin</code> and <code>manage.py</code> =
+
= django-admin manage.py =
  
<code>django-admin</code> is Django's command-line utility for administrative tasks.
+
<code>django-admin</code> Django 用于管理任务的命令行实用程序。 本文档概述了它可以做的所有事情。
This document outlines all it can do.
 
  
In addition, <code>manage.py</code> is automatically created in each Django project. It
+
另外,<code>manage.py</code> 是在每个 Django 项目中自动创建的。 它与 <code>django-admin</code> 执行相同的操作,但还设置了 <span id="index-0" class="target"></span>[[../../topics/settings#envvar-DJANGO_SETTINGS_MODULE|DJANGO_SETTINGS_MODULE]] 环境变量,使其指向您项目的 <code>settings.py</code> 文件。
does the same thing as <code>django-admin</code> but also sets the
 
<span id="index-0" class="target"></span>[[../../topics/settings#envvar-DJANGO_SETTINGS_MODULE|<code>DJANGO_SETTINGS_MODULE</code>]] environment variable so that it points to your
 
project's <code>settings.py</code> file.
 
  
The <code>django-admin</code> script should be on your system path if you installed
+
如果您通过 <code>pip</code> 安装了 Django,那么 <code>django-admin</code> 脚本应该在您的系统路径上。 如果它不在您的路径中,您可以在 Python 安装的 <code>site-packages/django/bin</code> 中找到它。 考虑从路径上的某个位置对其进行符号链接,例如 <code>/usr/local/bin</code>
Django via <code>pip</code>. If it's not on your path, you can find it in
 
<code>site-packages/django/bin</code> within your Python installation. Consider
 
symlinking it from some place on your path, such as <code>/usr/local/bin</code>.
 
  
For Windows users, who do not have symlinking functionality available, you can
+
对于没有可用符号链接功能的 Windows 用户,您可以将 <code>django-admin.exe</code> 复制到现有路径上的某个位置或编辑 <code>PATH</code> 设置(在 <code>Settings - Control Panel - System - Advanced - Environment...</code> 下)以指向它的安装位置。
copy <code>django-admin.exe</code> to a location on your existing path or edit the
 
<code>PATH</code> settings (under <code>Settings - Control Panel - System - Advanced - Environment...</code>) to point to its installed location.
 
  
Generally, when working on a single Django project, it's easier to use
+
通常,在处理单个 Django 项目时,使用 <code>manage.py</code> 比使用 <code>django-admin</code> 更容易。 如果您需要在多个 Django 设置文件之间切换,请使用 <code>django-admin</code> <span id="index-1" class="target"></span>[[../../topics/settings#envvar-DJANGO_SETTINGS_MODULE|DJANGO_SETTINGS_MODULE]] <code>--settings</code> 命令行选项。
<code>manage.py</code> than <code>django-admin</code>. If you need to switch between multiple
 
Django settings files, use <code>django-admin</code> with
 
<span id="index-1" class="target"></span>[[../../topics/settings#envvar-DJANGO_SETTINGS_MODULE|<code>DJANGO_SETTINGS_MODULE</code>]] or the [[#cmdoption-settings|<code>--settings</code>]] command line
 
option.
 
  
The command-line examples throughout this document use <code>django-admin</code> to
+
本文档中的命令行示例使用 <code>django-admin</code> 以保持一致,但任何示例也可以使用 <code>manage.py</code> <code>python -m django</code>
be consistent, but any example can use <code>manage.py</code> or <code>python -m django</code>
 
just as well.
 
  
 
<div id="usage" class="section">
 
<div id="usage" class="section">
  
== 用法 ==
+
== 如何使用 ==
  
<div class="highlight-console notranslate">
+
<code>command</code> 应该是本文档中列出的命令之一。 <code>options</code> 是可选的,应该是零个或多个可用于给定命令的选项。
 
 
<div class="highlight">
 
 
 
<pre>$ django-admin &lt;command&gt; [options]
 
$ manage.py &lt;command&gt; [options]
 
$ python -m django &lt;command&gt; [options]</pre>
 
 
 
</div>
 
 
 
</div>
 
<code>command</code> should be one of the commands listed in this document.
 
<code>options</code>, which is optional, should be zero or more of the options available
 
for the given command.
 
  
 
<div id="getting-runtime-help" class="section">
 
<div id="getting-runtime-help" class="section">
  
=== Getting runtime help ===
+
=== 获得运行时帮助 ===
 
 
; <code>django-admin help</code>
 
:
 
  
Run <code>django-admin help</code> to display usage information and a list of the
+
运行 <code>django-admin help</code> 以显示使用信息和每个应用程序提供的命令列表。
commands provided by each application.
 
  
Run <code>django-admin help --commands</code> to display a list of all available
+
运行 <code>django-admin help --commands</code> 以显示所有可用命令的列表。
commands.
 
  
Run <code>django-admin help &lt;command&gt;</code> to display a description of the given
+
运行 <code>django-admin help &lt;command&gt;</code> 以显示给定命令的描述及其可用选项列表。
command and a list of its available options.
 
  
  
第69行: 第36行:
 
<div id="app-names" class="section">
 
<div id="app-names" class="section">
  
=== App names ===
+
=== 应用名称 ===
  
Many commands take a list of &quot;app names.&quot; An &quot;app name&quot; is the basename of
+
许多命令采用“应用程序名称”列表。 “应用程序名称”是包含模型的包的基本名称。 例如,如果您的 [[#id1|:setting:`INSTALLED_APPS`]] 包含字符串 <code>'mysite.blog'</code>,则应用名称为 <code>blog</code>
the package containing your models. For example, if your [[../settings#std-setting-INSTALLED_APPS|<code>INSTALLED_APPS</code>]]
 
contains the string <code>'mysite.blog'</code>, the app name is <code>blog</code>.
 
  
  
第79行: 第44行:
 
<div id="determining-the-version" class="section">
 
<div id="determining-the-version" class="section">
  
=== Determining the version ===
+
=== 确定版本 ===
  
; <code>django-admin version</code>
+
运行 <code>django-admin version</code> 以显示当前的 Django 版本。
:
 
  
Run <code>django-admin version</code> to display the current Django version.
+
输出遵循 <span id="index-2" class="target"></span>[https://www.python.org/dev/peps/pep-0440 PEP 440] 中描述的架构:
 
 
The output follows the schema described in <span id="index-2" class="target"></span>[https://www.python.org/dev/peps/pep-0440 '''PEP 440''']:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第92行: 第54行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>1.4.dev17026
+
<syntaxhighlight lang="python">1.4.dev17026
 
1.4a1
 
1.4a1
1.4</pre>
+
1.4</syntaxhighlight>
  
 
</div>
 
</div>
第103行: 第65行:
 
<div id="displaying-debug-output" class="section">
 
<div id="displaying-debug-output" class="section">
  
=== Displaying debug output ===
+
=== 显示 debug 输出 ===
  
Use [[#cmdoption-verbosity|<code>--verbosity</code>]] to specify the amount of notification and debug
+
使用 <code>--verbosity</code> 指定 <code>django-admin</code> 打印到控制台的通知和调试信息的数量。
information that <code>django-admin</code> prints to the console.
 
  
  
第114行: 第75行:
 
<div id="available-commands" class="section">
 
<div id="available-commands" class="section">
  
== Available commands ==
+
== 可用命令 ==
  
 
<div id="check" class="section">
 
<div id="check" class="section">
  
=== <code>check</code> ===
+
=== check ===
  
; <code>django-admin check [app_label [app_label ...]]</code>
+
使用[[../checks|系统检查框架]]检查整个Django项目的常见问题。
:
 
  
Uses the [[../checks|<span class="doc">system check framework</span>]] to inspect the entire
+
默认情况下,将检查所有应用程序。 您可以通过提供应用标签列表作为参数来检查应用的子集:
Django project for common problems.
 
 
 
By default, all apps will be checked. You can check a subset of apps by
 
providing a list of app labels as arguments:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第133行: 第89行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin check auth admin myapp</pre>
+
<syntaxhighlight lang="python">django-admin check auth admin myapp</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If you do not specify any app, all apps will be checked.
+
如果您不指定任何应用程序,则将检查所有应用程序。
  
; <span id="cmdoption-check-t"></span><code>--tag</code><code> TAGS</code><code>, </code><code>-t</code><code> TAGS</code>
+
系统检查框架执行许多不同类型的检查,这些检查是 [[../checks#system-check-builtin-tags|用标签]] 分类的。 您可以使用这些标签将执行的检查限制为仅针对特定类别中的检查。 例如,要仅执行模型和兼容性检查,请运行:
:
 
 
 
The system check framework performs many different types of checks that are
 
[[../checks#system-check-builtin-tags|<span class="std std-ref">categorized with tags</span>]]. You can use these
 
tags to restrict the checks performed to just those in a particular category.
 
For example, to perform only models and compatibility checks, run:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第152行: 第102行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin check --tag models --tag compatibility</pre>
+
<syntaxhighlight lang="python">django-admin check --tag models --tag compatibility</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--list-tags</code><code></code>
+
列出所有可用的标签。
:
 
  
Lists all available tags.
+
激活一些仅在部署环境中相关的附加检查。
  
; <code>--deploy</code><code></code>
+
您可以在本地开发环境中使用此选项,但由于您的本地开发设置模块可能没有许多生产设置,您可能希望将 <code>check</code> 命令指向不同的设置模块,通过设置<code>DJANGO_SETTINGS_MODULE</code> 环境变量,或通过传递 <code>--settings</code> 选项:
:
 
 
 
Activates some additional checks that are only relevant in a deployment setting.
 
 
 
You can use this option in your local development environment, but since your
 
local development settings module may not have many of your production settings,
 
you will probably want to point the <code>check</code> command at a different settings
 
module, either by setting the <code>DJANGO_SETTINGS_MODULE</code> environment variable,
 
or by passing the <code>--settings</code> option:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第177行: 第117行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin check --deploy --settings=production_settings</pre>
+
<syntaxhighlight lang="python">django-admin check --deploy --settings=production_settings</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Or you could run it directly on a production or staging deployment to verify
+
或者您可以直接在生产或临时部署上运行它以验证是否使用了正确的设置(省略 <code>--settings</code>)。 您甚至可以将其作为集成测试套件的一部分。
that the correct settings are in use (omitting <code>--settings</code>). You could even
 
make it part of your integration test suite.
 
  
; <code>--fail-level</code><code> {CRITICAL,ERROR,WARNING,INFO,DEBUG}</code>
+
指定将导致命令以非零状态退出的消息级别。 默认值为 <code>ERROR</code>
:
 
 
 
Specifies the message level that will cause the command to exit with a non-zero
 
status. Default is <code>ERROR</code>.
 
  
  
第196行: 第130行:
 
<div id="compilemessages" class="section">
 
<div id="compilemessages" class="section">
  
=== <code>compilemessages</code> ===
+
=== compilemessages ===
  
; <code>django-admin compilemessages</code>
+
将 [[#id3|:djadmin:`makemessages`]] 创建的 <code>.po</code> 文件编译为 <code>.mo</code> 文件,以便与内置的 gettext 支持一起使用。 参见 [[../../topics/i18n/index|国际化和本地化]] 。
:
 
  
Compiles <code>.po</code> files created by [[#django-admin-makemessages|<code>makemessages</code>]] to <code>.mo</code> files for
+
指定要处理的语言环境。 如果未提供,则处理所有语言环境。
use with the built-in gettext support. See [[../../topics/i18n/index|<span class="doc">国际化和本地化</span>]].
 
  
; <span id="cmdoption-compilemessages-l"></span><code>--locale</code><code> LOCALE</code><code>, </code><code>-l</code><code> LOCALE</code>
+
指定要从处理中排除的区域设置。 如果未提供,则不排除任何区域设置。
:
 
 
 
Specifies the locale(s) to process. If not provided, all locales are processed.
 
 
 
; <span id="cmdoption-compilemessages-x"></span><code>--exclude</code><code> EXCLUDE</code><code>, </code><code>-x</code><code> EXCLUDE</code>
 
:
 
  
Specifies the locale(s) to exclude from processing. If not provided, no locales
+
包括 `fuzzy 翻译`_ 到编译文件。
are excluded.
 
 
 
; <span id="cmdoption-compilemessages-f"></span><code>--use-fuzzy</code><code></code><code>, </code><code>-f</code><code></code>
 
:
 
 
 
Includes fuzzy translations into compiled files.
 
  
 
用法示例:
 
用法示例:
第226行: 第146行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin compilemessages --locale=pt_BR
+
<syntaxhighlight lang="python">django-admin compilemessages --locale=pt_BR
 
django-admin compilemessages --locale=pt_BR --locale=fr -f
 
django-admin compilemessages --locale=pt_BR --locale=fr -f
 
django-admin compilemessages -l pt_BR
 
django-admin compilemessages -l pt_BR
第233行: 第153行:
 
django-admin compilemessages --exclude=pt_BR --exclude=fr
 
django-admin compilemessages --exclude=pt_BR --exclude=fr
 
django-admin compilemessages -x pt_BR
 
django-admin compilemessages -x pt_BR
django-admin compilemessages -x pt_BR -x fr</pre>
+
django-admin compilemessages -x pt_BR -x fr</syntaxhighlight>
  
 
</div>
 
</div>
第242行: 第162行:
 
<div id="createcachetable" class="section">
 
<div id="createcachetable" class="section">
  
=== <code>createcachetable</code> ===
+
=== createcachetable ===
 
 
; <code>django-admin createcachetable</code>
 
:
 
 
 
Creates the cache tables for use with the database cache backend using the
 
information from your settings file. See [[../../topics/cache|<span class="doc">Django 缓存框架</span>]] for more
 
information.
 
 
 
; <code>--database</code><code> DATABASE</code>
 
:
 
  
Specifies the database in which the cache table(s) will be created. Defaults to
+
使用设置文件中的信息创建用于数据库缓存后端的缓存表。 有关更多信息,请参阅 [[../../topics/cache|Django 的缓存框架]] 。
<code>default</code>.
 
  
; <code>--dry-run</code><code></code>
+
指定将在其中创建缓存表的数据库。 默认为 <code>default</code>
:
 
  
Prints the SQL that would be run without actually running it, so you can
+
打印无需实际运行的 SQL,所以你可以自定义它或使用迁移框架。
customize it or use the migrations framework.
 
  
  
第267行: 第174行:
 
<div id="dbshell" class="section">
 
<div id="dbshell" class="section">
  
=== <code>dbshell</code> ===
+
=== dbshell ===
  
; <code>django-admin dbshell</code>
+
为您的数据库引擎中指定的数据库引擎运行命令行客户端 [[#id5|:设置:`引擎 `]] 设置,使用您在 [[#id7|:设置:`用户`]] , [[#id9|:设置:`密码`]] 等,设置。
:
 
  
Runs the command-line client for the database engine specified in your
+
* 对于 PostgreSQL,它运行 <code>psql</code> 命令行客户端。
[[../settings#std-setting-DATABASE-ENGINE|<code>ENGINE</code>]] setting, with the connection parameters
+
* 对于 MySQL,这将运行 <code>mysql</code> 命令行客户端。
specified in your [[../settings#std-setting-USER|<code>USER</code>]], [[../settings#std-setting-PASSWORD|<code>PASSWORD</code>]], etc., settings.
+
* 对于 SQLite,它运行 <code>sqlite3</code> 命令行客户端。
 +
* 对于 Oracle,这将运行 <code>sqlplus</code> 命令行客户端。
  
* For PostgreSQL, this runs the <code>psql</code> command-line client.
+
此命令假定程序在您的 <code>PATH</code> 上,因此对程序名称的简单调用 (<code>psql</code><code>mysql</code><code>sqlite3</code><code>sqlplus</code> ]) 将在正确的位置找到该程序。 无法手动指定程序的位置。
* For MySQL, this runs the <code>mysql</code> command-line client.
 
* For SQLite, this runs the <code>sqlite3</code> command-line client.
 
* For Oracle, this runs the <code>sqlplus</code> command-line client.
 
  
This command assumes the programs are on your <code>PATH</code> so that a simple call to
+
指定要在其上打开 shell 的数据库。 默认为 <code>default</code>
the program name (<code>psql</code>, <code>mysql</code>, <code>sqlite3</code>, <code>sqlplus</code>) will find the
 
program in the right place. There's no way to specify the location of the
 
program manually.
 
 
 
; <code>--database</code><code> DATABASE</code>
 
:
 
 
 
Specifies the database onto which to open a shell. Defaults to <code>default</code>.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
Be aware that not all options set it in the [[../settings#std-setting-OPTIONS|<code>OPTIONS</code>]] part of your
+
请注意,并非所有在 [[#id13|:setting:`DATABASES`]] 中的数据库配置的 [[#id11|:setting:`OPTIONS`]] 部分中设置它的选项都会传递给命令行客户端,例如 <code>'isolation_level'</code>
database configuration in [[../settings#std-setting-DATABASES|<code>DATABASES</code>]] are passed to the
 
command-line client, e.g. <code>'isolation_level'</code>.
 
  
  
第305行: 第199行:
 
<div id="diffsettings" class="section">
 
<div id="diffsettings" class="section">
  
=== <code>diffsettings</code> ===
+
=== diffsettings ===
 
 
; <code>django-admin diffsettings</code>
 
:
 
 
 
Displays differences between the current settings file and Django's default
 
settings (or another settings file specified by [[#cmdoption-diffsettings-default|<code>--default</code>]]).
 
 
 
Settings that don't appear in the defaults are followed by <code>&quot;###&quot;</code>. For
 
example, the default settings don't define [[../settings#std-setting-ROOT_URLCONF|<code>ROOT_URLCONF</code>]], so
 
[[../settings#std-setting-ROOT_URLCONF|<code>ROOT_URLCONF</code>]] is followed by <code>&quot;###&quot;</code> in the output of
 
<code>diffsettings</code>.
 
  
; <code>--all</code><code></code>
+
显示当前设置文件和 Django 的默认设置(或由 <code>--default</code> 指定的另一个设置文件)之间的差异。
:
 
  
Displays all settings, even if they have Django's default value. Such settings
+
默认值中没有出现的设置后面跟着<code>&quot;###&quot;</code>。 例如,默认设置没有定义 [[#id15|:setting:`ROOT_URLCONF`]],所以 [[#id17|:setting:`ROOT_URLCONF`]] 在 <code>&quot;###&quot;</code> X159X]。
are prefixed by <code>&quot;###&quot;</code>.
 
  
; <code>--default</code><code> MODULE</code>
+
显示所有设置,即使它们具有 Django 的默认值。 此类设置以 <code>&quot;###&quot;</code> 为前缀。
:
 
  
The settings module to compare the current settings against. Leave empty to
+
用于比较当前设置的设置模块。 留空以与 Django 的默认设置进行比较。
compare against Django's default settings.
 
  
; <code>--output</code><code> {hash,unified}</code>
+
指定输出格式。 可用值为 <code>hash</code> <code>unified</code><code>hash</code> 是显示上述输出的默认模式。 <code>unified</code> 显示类似于 <code>diff -u</code> 的输出。 默认设置以减号为前缀,后跟以加号为前缀的更改设置。
:
 
 
 
Specifies the output format. Available values are <code>hash</code> and <code>unified</code>.
 
<code>hash</code> is the default mode that displays the output that's described above.
 
<code>unified</code> displays the output similar to <code>diff -u</code>. Default settings are
 
prefixed with a minus sign, followed by the changed setting prefixed with a
 
plus sign.
 
  
  
第343行: 第215行:
 
<div id="dumpdata" class="section">
 
<div id="dumpdata" class="section">
  
=== <code>dumpdata</code> ===
+
=== dumpdata ===
  
; <code>django-admin dumpdata [app_label[.ModelName] [app_label[.ModelName] ...]]</code>
+
将数据库中与指定应用程序相关联的所有数据输出到标准输出。
:
 
  
Outputs to standard output all data in the database associated with the named
+
如果没有提供应用程序名称,所有安装的应用程序将被转储。
application(s).
 
  
If no application name is provided, all installed applications will be dumped.
+
<code>dumpdata</code> 的输出可以用作 [[#id19|:djadmin:`loaddata`]] 的输入。
  
The output of <code>dumpdata</code> can be used as input for [[#django-admin-loaddata|<code>loaddata</code>]].
+
请注意,<code>dumpdata</code> 使用模型上的默认管理器来选择要转储的记录。 如果您使用 [[../../topics/db/managers#custom-managers|自定义管理器]] 作为默认管理器并且它过滤了一些可用记录,则并非所有对象都将被转储。
  
Note that <code>dumpdata</code> uses the default manager on the model for selecting the
+
使用 Django 的基本管理器,转储可能会被自定义管理器过滤或修改的记录。
records to dump. If you're using a [[../../topics/db/managers#custom-managers|<span class="std std-ref">custom manager</span>]] as
 
the default manager and it filters some of the available records, not all of the
 
objects will be dumped.
 
  
; <span id="cmdoption-dumpdata-a"></span><code>--all</code><code></code><code>, </code><code>-a</code><code></code>
+
指定输出的序列化格式。 默认为 JSON。 支持的格式在[[../../topics/serialization#serialization-formats|序列化格式]]中列出。
:
 
 
 
Uses Django's base manager, dumping records which might otherwise be filtered
 
or modified by a custom manager.
 
  
; <code>--format</code><code> FORMAT</code>
+
指定要在输出中使用的缩进空格数。 默认为 <code>None</code>,在一行上显示所有数据。
:
 
  
Specifies the serialization format of the output. Defaults to JSON. Supported
+
防止转储特定的应用程序或模型(以 <code>app_label.ModelName</code> 的形式指定)。 如果您指定模型名称,则输出将仅限于该模型,而不是整个应用程序。 您还可以混合应用程序名称和型号名称。
formats are listed in [[../../topics/serialization#serialization-formats|<span class="std std-ref">Serialization formats</span>]].
 
  
; <code>--indent</code><code> INDENT</code>
+
如果要排除多个应用程序,请多次传递 <code>--exclude</code>
:
 
 
 
Specifies the number of indentation spaces to use in the output. Defaults to
 
<code>None</code> which displays all data on single line.
 
 
 
; <span id="cmdoption-dumpdata-e"></span><code>--exclude</code><code> EXCLUDE</code><code>, </code><code>-e</code><code> EXCLUDE</code>
 
:
 
 
 
Prevents specific applications or models (specified in the form of
 
<code>app_label.ModelName</code>) from being dumped. If you specify a model name, the
 
output will be restricted to that model, rather than the entire application.
 
You can also mix application names and model names.
 
 
 
If you want to exclude multiple applications, pass <code>--exclude</code> more than
 
once:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第393行: 第239行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin dumpdata --exclude=auth --exclude=contenttypes</pre>
+
<syntaxhighlight lang="python">django-admin dumpdata --exclude=auth --exclude=contenttypes</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--database</code><code> DATABASE</code>
+
指定将转储数据的数据库。 默认为 <code>default</code>
:
 
  
Specifies the database from which data will be dumped. Defaults to <code>default</code>.
+
使用 <code>natural_key()</code> 模型方法将任何外键和多对多关系序列化到定义该方法的类型的对象。 如果您要转储 <code>contrib.auth</code> <code>Permission</code> 对象或 <code>contrib.contenttypes</code> <code>ContentType</code> 对象,您可能应该使用此标志。 有关此选项和下一个选项的更多详细信息,请参阅 [[../../topics/serialization#topics-serialization-natural-keys|自然键]] 文档。
  
; <code>--natural-foreign</code><code></code>
+
省略该对象序列化数据中的主键,因为它可以在反序列化过程中计算。
:
 
  
Uses the <code>natural_key()</code> model method to serialize any foreign key and
+
仅输出由逗号分隔的主键列表指定的对象。 这仅在倾销一种模型时可用。 默认输出模型的所有记录。
many-to-many relationship to objects of the type that defines the method. If
 
you're dumping <code>contrib.auth</code> <code>Permission</code> objects or
 
<code>contrib.contenttypes</code> <code>ContentType</code> objects, you should probably use this
 
flag. See the [[../../topics/serialization#topics-serialization-natural-keys|<span class="std std-ref">natural keys</span>]]
 
documentation for more details on this and the next option.
 
  
; <code>--natural-primary</code><code></code>
+
指定要写入序列化数据的文件。 默认情况下,数据进入标准输出。
:
 
  
Omits the primary key in the serialized data of this object since it can be
+
当设置此选项且 <code>--verbosity</code> 大于 0(默认)时,终端中会显示进度条。
calculated during deserialization.
 
 
 
; <code>--pks</code><code> PRIMARY_KEYS</code>
 
:
 
 
 
Outputs only the objects specified by a comma separated list of primary keys.
 
This is only available when dumping one model. By default, all the records of
 
the model are output.
 
 
 
; <span id="cmdoption-dumpdata-o"></span><code>--output</code><code> OUTPUT</code><code>, </code><code>-o</code><code> OUTPUT</code>
 
:
 
 
 
Specifies a file to write the serialized data to. By default, the data goes to
 
standard output.
 
 
 
When this option is set and <code>--verbosity</code> is greater than 0 (the default), a
 
progress bar is shown in the terminal.
 
  
  
第439行: 第260行:
 
<div id="flush" class="section">
 
<div id="flush" class="section">
  
=== <code>flush</code> ===
+
=== flush ===
 
 
; <code>django-admin flush</code>
 
:
 
 
 
Removes all data from the database and re-executes any post-synchronization
 
handlers. The table of which migrations have been applied is not cleared.
 
  
If you would rather start from an empty database and re-run all migrations, you
+
从数据库中删除所有数据并重新执行任何同步后处理程序。 已应用迁移的表不会被清除。
should drop and recreate the database and then run [[#django-admin-migrate|<code>migrate</code>]] instead.
 
  
; <span id="cmdoption-flush-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
+
如果您宁愿从空数据库开始并重新运行所有迁移,您应该删除并重新创建数据库,然后运行 [[#id21|:djadmin:`migrate`]]。
:  
 
  
Suppresses all user prompts.
+
禁止所有的用户提示。
  
; <code>--database</code><code> DATABASE</code>
+
指定要刷新的数据库。 默认为 <code>default</code>
:
 
 
 
Specifies the database to flush. Defaults to <code>default</code>.
 
  
  
第464行: 第274行:
 
<div id="inspectdb" class="section">
 
<div id="inspectdb" class="section">
  
=== <code>inspectdb</code> ===
+
=== inspectdb ===
  
; <code>django-admin inspectdb [table [table ...]]</code>
+
内省 [[#id23|:setting:`NAME`]] 设置指向的数据库中的数据库表,并将 Django 模型模块(<code>models.py</code> 文件)输出到标准输出。
:
 
  
Introspects the database tables in the database pointed-to by the
+
您可以通过将名称作为参数传递来选择要检查的表或视图。 如果未提供参数,则仅在使用 <code>--include-views</code> 选项时才为视图创建模型。 如果使用 <code>--include-partitions</code> 选项,则在 PostgreSQL 上创建分区表模型。
[[../settings#std-setting-NAME|<code>NAME</code>]] setting and outputs a Django model module (a <code>models.py</code>
 
file) to standard output.
 
  
You may choose what tables or views to inspect by passing their names as
+
如果您有一个要使用 Django 的旧数据库,请使用此选项。 该脚本将检查数据库并为其中的每个表创建一个模型。
arguments. If no arguments are provided, models are created for views only if
 
the [[#cmdoption-inspectdb-include-views|<code>--include-views</code>]] option is used. Models for partition tables are
 
created on PostgreSQL if the [[#cmdoption-inspectdb-include-partitions|<code>--include-partitions</code>]] option is used.
 
  
Use this if you have a legacy database with which you'd like to use Django.
+
正如您所料,创建的模型将具有表中每个字段的属性。 请注意, <code>inspectdb</code> 在其字段名输出中有一些特殊情况:
The script will inspect the database and create a model for each table within
 
it.
 
  
As you might expect, the created models will have an attribute for every field
+
* 如果 <code>inspectdb</code> 无法将列的类型映射到模型字段类型,它将使用 <code>TextField</code> 并将在生成的模型中的字段旁边插入 Python 注释 <code>'This field type is a guess.'</code>。 识别的字段可能取决于 [[#id25|:setting:`INSTALLED_APPS`]] 中列出的应用程序。 例如,[[../contrib/postgres/index#module-django.contrib|django.contrib.postgres]] 添加了对几种 PostgreSQL 特定字段类型的识别。
in the table. Note that <code>inspectdb</code> has a few special cases in its field-name
+
* 如果数据库列名是 Python 保留字(例如 <code>'pass'</code>、<code>'class'</code> 或 <code>'for'</code>),则 <code>inspectdb</code> 将附加 <code>'_field'</code>属性名称。 例如,如果一个表有一个列 <code>'for'</code>,生成的模型将有一个字段 <code>'for_field'</code>,<code>db_column</code> 属性设置为 <code>'for'</code>。 <code>inspectdb</code> 将在字段旁边插入 Python 注释 <code>'Field renamed because it was a Python reserved word.'</code>。
output:
 
  
* If <code>inspectdb</code> cannot map a column's type to a model field type, it'll use <code>TextField</code> and will insert the Python comment <code>'This field type is a guess.'</code> next to the field in the generated model. The recognized fields may depend on apps listed in [[../settings#std-setting-INSTALLED_APPS|<code>INSTALLED_APPS</code>]]. For example, [[../contrib/postgres/index#module-django.contrib|<code>django.contrib.postgres</code>]] adds recognition for several PostgreSQL-specific field types.
+
此功能是一种快捷方式,而不是确定的模型生成。 运行后,您需要自己查看生成的模型以进行自定义。 特别是,您需要重新排列模型的顺序,以便正确排列引用其他模型的模型。
* If the database column name is a Python reserved word (such as <code>'pass'</code>, <code>'class'</code> or <code>'for'</code>), <code>inspectdb</code> will append <code>'_field'</code> to the attribute name. For example, if a table has a column <code>'for'</code>, the generated model will have a field <code>'for_field'</code>, with the <code>db_column</code> attribute set to <code>'for'</code>. <code>inspectdb</code> will insert the Python comment <code>'Field renamed because it was a Python reserved word.'</code> next to the field.
 
  
This feature is meant as a shortcut, not as definitive model generation. After
+
当在模型字段上指定 [[../models/fields#django.db.models.Field|default]] 时,Django 不会创建数据库默认值。 同样,数据库默认值不会转换为模型字段默认值,也不会被 <code>inspectdb</code> 以任何方式检测到。
you run it, you'll want to look over the generated models yourself to make
 
customizations. In particular, you'll need to rearrange models' order, so that
 
models that refer to other models are ordered properly.
 
  
Django doesn't create database defaults when a
+
默认情况下,<code>inspectdb</code> 创建非托管模型。 也就是说,模型的 <code>Meta</code> 类中的 <code>managed = False</code> 告诉 Django 不要管理每个表的创建、修改和删除。 如果您确实想让 Django 管理表的生命周期,则需要将 [[../models/options#django.db.models.Options|managed]] 选项更改为 <code>True</code>(或者简单地将其删除,因为 <code>True</code> 是其默认值价值)。
[[../models/fields#django.db.models.Field|<code>default</code>]] is specified on a model field.
 
Similarly, database defaults aren't translated to model field defaults or
 
detected in any fashion by <code>inspectdb</code>.
 
 
 
By default, <code>inspectdb</code> creates unmanaged models. That is, <code>managed = False</code>
 
in the model's <code>Meta</code> class tells Django not to manage each table's creation,
 
modification, and deletion. If you do want to allow Django to manage the
 
table's lifecycle, you'll need to change the
 
[[../models/options#django.db.models.Options|<code>managed</code>]] option to <code>True</code> (or simply remove
 
it because <code>True</code> is its default value).
 
  
 
<div id="database-specific-notes" class="section">
 
<div id="database-specific-notes" class="section">
第514行: 第301行:
 
===== Oracle =====
 
===== Oracle =====
  
* Models are created for materialized views if [[#cmdoption-inspectdb-include-views|<code>--include-views</code>]] is used.
+
* 如果使用 <code>--include-views</code>,则为实体化视图创建模型。
  
  
第522行: 第309行:
 
===== PostgreSQL =====
 
===== PostgreSQL =====
  
* Models are created for foreign tables.
+
* 为外部表创建模型。
* Models are created for materialized views if [[#cmdoption-inspectdb-include-views|<code>--include-views</code>]] is used.
+
* 如果使用 <code>--include-views</code>,则为实体化视图创建模型。
* Models are created for partition tables if [[#cmdoption-inspectdb-include-partitions|<code>--include-partitions</code>]] is used.
+
* 如果使用 <code>--include-partitions</code>,则为分区表创建模型。
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
Support for foreign tables and materialized views was added.
+
<span class="versionmodified changed"> 2.2 版本变化: </span> 添加了对外部表和物化视图的支持。
  
  
 
</div>
 
</div>
; <code>--database</code><code> DATABASE</code>
+
指定要内省的数据库。 默认为 <code>default</code>
:
 
 
 
Specifies the database to introspect. Defaults to <code>default</code>.
 
 
 
; <code>--include-partitions</code><code></code>
 
:
 
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
If this option is provided, models are also created for partitions.
+
如果提供了这个选项,也会为分区创建模型。
  
Only support for PostgreSQL is implemented.
+
只实现了对 PostgreSQL 的支持。
 
 
; <code>--include-views</code><code></code>
 
:
 
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.1 版中的新功能。</span>
  
  
 
</div>
 
</div>
If this option is provided, models are also created for database views.
+
如果提供了这个选项,也会为数据库视图创建模型。
  
  
第567行: 第347行:
 
<div id="loaddata" class="section">
 
<div id="loaddata" class="section">
  
=== <code>loaddata</code> ===
+
=== loaddata ===
  
; <code>django-admin loaddata fixture [fixture ...]</code>
+
搜索并将已命名固定数据中的内容加载到数据库中。
:
 
  
Searches for and loads the contents of the named fixture into the database.
+
指定数据将加载到的数据库。 默认为 <code>default</code>。
  
; <code>--database</code><code> DATABASE</code>
+
忽略自固定数据最初生成以来可能已经被删除的字段和模型。
:
 
  
Specifies the database into which the data will be loaded. Defaults to
+
指定一个单一的应用来寻找固定数据,而不是在所有的应用程序中寻找。
<code>default</code>.
 
  
; <span id="cmdoption-loaddata-i"></span><code>--ignorenonexistent</code><code></code><code>, </code><code>-i</code><code></code>
+
为从 stdin [[#loading-fixtures-stdin|读取的装置]] 指定 [[../../topics/serialization#serialization-formats|序列化格式]] (例如,<code>json</code> <code>xml</code>)。
:
 
 
 
Ignores fields and models that may have been removed since the fixture was
 
originally generated.
 
 
 
; <code>--app</code><code> APP_LABEL</code>
 
:
 
  
Specifies a single app to look for fixtures in rather than looking in all apps.
+
不包括从给定的应用程序和/或模型(以 <code>app_label</code> <code>app_label.ModelName</code> 的形式)加载灯具。 多次使用该选项以排除多个应用程序或模型。
 
 
; <code>--format</code><code> FORMAT</code>
 
:
 
 
 
Specifies the [[../../topics/serialization#serialization-formats|<span class="std std-ref">serialization format</span>]] (e.g.,
 
<code>json</code> or <code>xml</code>) for fixtures [[#loading-fixtures-stdin|<span class="std std-ref">read from stdin</span>]].
 
 
 
; <span id="cmdoption-loaddata-e"></span><code>--exclude</code><code> EXCLUDE</code><code>, </code><code>-e</code><code> EXCLUDE</code>
 
:
 
 
 
Excludes loading the fixtures from the given applications and/or models (in the
 
form of <code>app_label</code> or <code>app_label.ModelName</code>). Use the option multiple
 
times to exclude more than one app or model.
 
  
 
<div id="what-s-a-fixture" class="section">
 
<div id="what-s-a-fixture" class="section">
  
==== What's a &quot;fixture&quot;? ====
+
==== 什么是“夹具”? ====
  
A ''fixture'' is a collection of files that contain the serialized contents of
+
''fixture'' 是包含数据库序列化内容的文件集合。 每个夹具都有一个唯一的名称,构成夹具的文件可以分布在多个目录、多个应用程序中。
the database. Each fixture has a unique name, and the files that comprise the
 
fixture can be distributed over multiple directories, in multiple applications.
 
  
Django will search in three locations for fixtures:
+
Django 会在三个位置搜索固定数据:
  
# In the <code>fixtures</code> directory of every installed application
+
# 在每个已安装应用程序的 <code>fixtures</code> 目录中
# In any directory named in the [[../settings#std-setting-FIXTURE_DIRS|<code>FIXTURE_DIRS</code>]] setting
+
# [[#id27|:setting:`FIXTURE_DIRS`]] 设置中命名的任何目录中
# In the literal path named by the fixture
+
# 在由固定数据命名的文字路径中
  
Django will load any and all fixtures it finds in these locations that match
+
Django 将加载它在这些位置找到的与所提供的固定数据名称相匹配的任何和所有固定数据。
the provided fixture names.
 
  
If the named fixture has a file extension, only fixtures of that type
+
如果命名装置具有文件扩展名,则只会加载该类型的装置。 例如:
will be loaded. For example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第628行: 第381行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin loaddata mydata.json</pre>
+
<syntaxhighlight lang="python">django-admin loaddata mydata.json</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would only load JSON fixtures called <code>mydata</code>. The fixture extension
+
只会加载名为 <code>mydata</code> 的 JSON 固定装置。 夹具扩展名必须对应于 [[../../topics/serialization#serialization-formats|串行器]] 的注册名称(例如,<code>json</code> <code>xml</code>)。
must correspond to the registered name of a
 
[[../../topics/serialization#serialization-formats|<span class="std std-ref">serializer</span>]] (e.g., <code>json</code> or <code>xml</code>).
 
  
If you omit the extensions, Django will search all available fixture types
+
如果省略扩展名,Django 将搜索所有可用的设备类型以查找匹配的设备。 例如:
for a matching fixture. For example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第644行: 第394行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin loaddata mydata</pre>
+
<syntaxhighlight lang="python">django-admin loaddata mydata</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would look for any fixture of any fixture type called <code>mydata</code>. If a fixture
+
将寻找任何名为 <code>mydata</code> 的夹具类型的任何夹具。 如果夹具目录包含 <code>mydata.json</code>,则该夹具将作为 JSON 夹具加载。
directory contained <code>mydata.json</code>, that fixture would be loaded
 
as a JSON fixture.
 
  
The fixtures that are named can include directory components. These
+
命名的装置可以包含目录组件。 这些目录将包含在搜索路径中。 例如:
directories will be included in the search path. For example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第660行: 第407行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin loaddata foo/bar/mydata.json</pre>
+
<syntaxhighlight lang="python">django-admin loaddata foo/bar/mydata.json</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would search <code>&lt;app_label&gt;/fixtures/foo/bar/mydata.json</code> for each installed
+
将为每个已安装的应用程序搜索 <code>&lt;app_label&gt;/fixtures/foo/bar/mydata.json</code>,为 [[#id29|:setting:`FIXTURE_DIRS`]] 中的每个目录搜索 <code>&lt;dirname&gt;/foo/bar/mydata.json</code>,以及文字路径 <code>foo/bar/mydata.json</code>
application, <code>&lt;dirname&gt;/foo/bar/mydata.json</code> for each directory in
 
[[../settings#std-setting-FIXTURE_DIRS|<code>FIXTURE_DIRS</code>]], and the literal path <code>foo/bar/mydata.json</code>.
 
  
When fixture files are processed, the data is saved to the database as is.
+
处理夹具文件时,数据按原样保存到数据库中。 模型定义的 [[../models/instances#django.db.models.Model|save()]] 方法不会被调用,并且任何 [[../signals#django.db.models.signals|pre_save]] [[../signals#django.db.models.signals|post_save]] 信号将使用 <code>raw=True</code> 调用,因为实例只包含属性是模型本地的。 例如,您可能想要禁用访问在夹具加载期间不存在的相关字段的处理程序,否则会引发异常:
Model defined [[../models/instances#django.db.models.Model|<code>save()</code>]] methods are not called, and
 
any [[../signals#django.db.models.signals|<code>pre_save</code>]] or
 
[[../signals#django.db.models.signals|<code>post_save</code>]] signals will be called with
 
<code>raw=True</code> since the instance only contains attributes that are local to the
 
model. You may, for example, want to disable handlers that access
 
related fields that aren't present during fixture loading and would otherwise
 
raise an exception:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第682行: 第420行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.db.models.signals import post_save
+
<syntaxhighlight lang="python">from django.db.models.signals import post_save
 
from .models import MyModel
 
from .models import MyModel
  
第691行: 第429行:
 
     ...
 
     ...
  
post_save.connect(my_handler, sender=MyModel)</pre>
+
post_save.connect(my_handler, sender=MyModel)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
You could also write a simple decorator to encapsulate this logic:
+
您还可以编写一个简单的装饰器来封装此逻辑:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第702行: 第440行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from functools import wraps
+
<syntaxhighlight lang="python">from functools import wraps
  
 
def disable_for_loaddata(signal_handler):
 
def disable_for_loaddata(signal_handler):
     &quot;&quot;&quot;
+
     """
 
     Decorator that turns off signal handlers when loading fixture data.
 
     Decorator that turns off signal handlers when loading fixture data.
     &quot;&quot;&quot;
+
     """
 
     @wraps(signal_handler)
 
     @wraps(signal_handler)
 
     def wrapper(*args, **kwargs):
 
     def wrapper(*args, **kwargs):
第717行: 第455行:
 
@disable_for_loaddata
 
@disable_for_loaddata
 
def my_handler(**kwargs):
 
def my_handler(**kwargs):
     ...</pre>
+
     ...</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Just be aware that this logic will disable the signals whenever fixtures are
+
请注意,此逻辑将在灯具反序列化时禁用信号,而不仅仅是在 <code>loaddata</code> 期间。
deserialized, not just during <code>loaddata</code>.
 
  
Note that the order in which fixture files are processed is undefined. However,
+
请注意,夹具文件的处理顺序是未定义的。 但是,所有夹具数据都作为单个事务安装,因此一个夹具中的数据可以引用另一个夹具中的数据。 如果数据库后端支持行级约束,则会在事务结束时检查这些约束。
all fixture data is installed as a single transaction, so data in
 
one fixture can reference data in another fixture. If the database backend
 
supports row-level constraints, these constraints will be checked at the
 
end of the transaction.
 
  
The [[#django-admin-dumpdata|<code>dumpdata</code>]] command can be used to generate input for <code>loaddata</code>.
+
[[#id31|:djadmin:`dumpdata`]] 命令可用于为 <code>loaddata</code> 生成输入。
  
  
第737行: 第470行:
 
<div id="compressed-fixtures" class="section">
 
<div id="compressed-fixtures" class="section">
  
==== Compressed fixtures ====
+
==== 压缩的固定数据 ====
  
Fixtures may be compressed in <code>zip</code>, <code>gz</code>, or <code>bz2</code> format. For example:
+
灯具可以压缩为 <code>zip</code><code>gz</code> <code>bz2</code> 格式。 例如:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第745行: 第478行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin loaddata mydata.json</pre>
+
<syntaxhighlight lang="python">django-admin loaddata mydata.json</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would look for any of <code>mydata.json</code>, <code>mydata.json.zip</code>,
+
将查找 <code>mydata.json</code><code>mydata.json.zip</code><code>mydata.json.gz</code> <code>mydata.json.bz2</code> 中的任何一个。 使用 zip 压缩存档中包含的第一个文件。
<code>mydata.json.gz</code>, or <code>mydata.json.bz2</code>. The first file contained within a
 
zip-compressed archive is used.
 
  
Note that if two fixtures with the same name but different
+
请注意,如果发现两个同名但灯具类型不同的灯具(例如,如果在同一灯具目录中发现 <code>mydata.json</code> <code>mydata.xml.gz</code>),则灯具安装将被中止,并且任何在调用 <code>loaddata</code> 时安装的数据将从数据库中删除。
fixture type are discovered (for example, if <code>mydata.json</code> and
 
<code>mydata.xml.gz</code> were found in the same fixture directory), fixture
 
installation will be aborted, and any data installed in the call to
 
<code>loaddata</code> will be removed from the database.
 
  
 
<div class="admonition-mysql-with-myisam-and-fixtures admonition">
 
<div class="admonition-mysql-with-myisam-and-fixtures admonition">
  
MySQL with MyISAM and fixtures
+
使用 MyISAM 的 MySQL 与固定数据
  
The MyISAM storage engine of MySQL doesn't support transactions or
+
MySQL MyISAM 存储引擎不支持事务或约束,因此如果您使用 MyISAM,您将不会得到夹具数据的验证,或者如果找到多个事务文件则回滚。
constraints, so if you use MyISAM, you won't get validation of fixture
 
data, or a rollback if multiple transaction files are found.
 
  
  
第774行: 第499行:
 
<div id="database-specific-fixtures" class="section">
 
<div id="database-specific-fixtures" class="section">
  
==== Database-specific fixtures ====
+
==== 特定数据库的固定数据 ====
  
If you're in a multi-database setup, you might have fixture data that
+
如果您处于多数据库设置中,您可能希望将夹具数据加载到一个数据库中,但不想加载到另一个数据库中。 在这种情况下,您可以将数据库标识符添加到灯具的名称中。
you want to load onto one database, but not onto another. In this
 
situation, you can add a database identifier into the names of your fixtures.
 
  
For example, if your [[../settings#std-setting-DATABASES|<code>DATABASES</code>]] setting has a 'master' database
+
例如,如果您的 [[#id33|:setting:`DATABASES`]] 设置定义了一个“主”数据库,则将夹具命名为 <code>mydata.master.json</code> <code>mydata.master.json.gz</code>,并且夹具将仅在您指定要将数据加载到 <code>master</code> 数据库中。
defined, name the fixture <code>mydata.master.json</code> or
 
<code>mydata.master.json.gz</code> and the fixture will only be loaded when you
 
specify you want to load data into the <code>master</code> database.
 
  
  
第790行: 第510行:
  
 
<span id="loading-fixtures-stdin"></span>
 
<span id="loading-fixtures-stdin"></span>
==== Loading fixtures from <code>stdin</code> ====
+
==== stdin 加载夹具 ====
  
You can use a dash as the fixture name to load input from <code>sys.stdin</code>. For
+
您可以使用破折号作为灯具名称来加载来自 <code>sys.stdin</code> 的输入。 例如:
example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第799行: 第518行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin loaddata --format=json -</pre>
+
<syntaxhighlight lang="python">django-admin loaddata --format=json -</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
When reading from <code>stdin</code>, the [[#cmdoption-loaddata-format|<code>--format</code>]] option
+
<code>stdin</code>读取时,需要<code>--format</code>选项来指定输入的[[../../topics/serialization#serialization-formats|序列化格式]](例如,<code>json</code><code>xml</code> )。
is required to specify the [[../../topics/serialization#serialization-formats|<span class="std std-ref">serialization format</span>]]
 
of the input (e.g., <code>json</code> or <code>xml</code>).
 
  
Loading from <code>stdin</code> is useful with standard input and output redirections.
+
<code>stdin</code> 加载对于标准输入和输出重定向很有用。 例如:
For example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第815行: 第531行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin dumpdata --format=json --database=test app_label.ModelName | django-admin loaddata --format=json --database=prod -</pre>
+
<syntaxhighlight lang="python">django-admin dumpdata --format=json --database=test app_label.ModelName | django-admin loaddata --format=json --database=prod -</syntaxhighlight>
  
 
</div>
 
</div>
第826行: 第542行:
 
<div id="makemessages" class="section">
 
<div id="makemessages" class="section">
  
=== <code>makemessages</code> ===
+
=== makemessages ===
 
 
; <code>django-admin makemessages</code>
 
:
 
  
Runs over the entire source tree of the current directory and pulls out all
+
遍历当前目录的整个源代码树并提取所有标记为翻译的字符串。 它在 conf/locale(在 Django 树中)或 locale(用于项目和应用程序)目录中创建(或更新)一个消息文件。 对消息文件进行更改后,您需要使用 [[#id35|:djadmin:`compilemessages`]] 编译它们,以便与内置的 gettext 支持一起使用。 有关详细信息,请参阅 [[../../topics/i18n/translation#how-to-create-language-files|i18n 文档]]
strings marked for translation. It creates (or updates) a message file in the
 
conf/locale (in the Django tree) or locale (for project and application)
 
directory. After making changes to the messages files you need to compile them
 
with [[#django-admin-compilemessages|<code>compilemessages</code>]] for use with the builtin gettext support. See
 
the [[../../topics/i18n/translation#how-to-create-language-files|<span class="std std-ref">i18n documentation</span>]] for details.
 
  
This command doesn't require configured settings. However, when settings aren't
+
此命令不需要配置设置。 但是,当没有配置设置时,该命令不能忽略 [[#id37|:setting:`MEDIA_ROOT`]] [[#id39|:setting:`STATIC_ROOT`]] 目录或包含 [[#id41|:setting: `LOCALE_PATHS`]]
configured, the command can't ignore the [[../settings#std-setting-MEDIA_ROOT|<code>MEDIA_ROOT</code>]] and
 
[[../settings#std-setting-STATIC_ROOT|<code>STATIC_ROOT</code>]] directories or include [[../settings#std-setting-LOCALE_PATHS|<code>LOCALE_PATHS</code>]].
 
  
; <span id="cmdoption-makemessages-a"></span><code>--all</code><code></code><code>, </code><code>-a</code><code></code>
+
更新所有可用语言的消息文件。
:
 
  
Updates the message files for all available languages.
+
指定要检查的文件扩展名列表(默认值:<code>html</code><code>txt</code><code>py</code> <code>js</code> 如果 <code>--domain</code> 是 [ X122X])。
 
 
; <span id="cmdoption-makemessages-e"></span><code>--extension</code><code> EXTENSIONS</code><code>, </code><code>-e</code><code> EXTENSIONS</code>
 
:
 
 
 
Specifies a list of file extensions to examine (default: <code>html</code>, <code>txt</code>,
 
<code>py</code> or <code>js</code> if [[#cmdoption-makemessages-domain|<code>--domain</code>]] is <code>js</code>).
 
  
 
用法示例:
 
用法示例:
第859行: 第558行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin makemessages --locale=de --extension xhtml</pre>
+
<syntaxhighlight lang="python">django-admin makemessages --locale=de --extension xhtml</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Separate multiple extensions with commas or use <code>-e</code> or <code>--extension</code>
+
用逗号分隔多个扩展名或多次使用 <code>-e</code> <code>--extension</code>
multiple times:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第871行: 第569行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin makemessages --locale=de --extension=html,txt --extension xml</pre>
+
<syntaxhighlight lang="python">django-admin makemessages --locale=de --extension=html,txt --extension xml</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <span id="cmdoption-makemessages-l"></span><code>--locale</code><code> LOCALE</code><code>, </code><code>-l</code><code> LOCALE</code>
+
指定要处理的 locale。
:
 
 
 
Specifies the locale(s) to process.
 
 
 
; <span id="cmdoption-makemessages-x"></span><code>--exclude</code><code> EXCLUDE</code><code>, </code><code>-x</code><code> EXCLUDE</code>
 
:
 
  
Specifies the locale(s) to exclude from processing. If not provided, no locales
+
指定要从处理中排除的区域设置。 如果未提供,则不排除任何区域设置。
are excluded.
 
  
 
用法示例:
 
用法示例:
第893行: 第584行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin makemessages --locale=pt_BR
+
<syntaxhighlight lang="python">django-admin makemessages --locale=pt_BR
 
django-admin makemessages --locale=pt_BR --locale=fr
 
django-admin makemessages --locale=pt_BR --locale=fr
 
django-admin makemessages -l pt_BR
 
django-admin makemessages -l pt_BR
第900行: 第591行:
 
django-admin makemessages --exclude=pt_BR --exclude=fr
 
django-admin makemessages --exclude=pt_BR --exclude=fr
 
django-admin makemessages -x pt_BR
 
django-admin makemessages -x pt_BR
django-admin makemessages -x pt_BR -x fr</pre>
+
django-admin makemessages -x pt_BR -x fr</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <span id="cmdoption-makemessages-d"></span><code>--domain</code><code> DOMAIN</code><code>, </code><code>-d</code><code> DOMAIN</code>
+
指定消息文件的域。 支持的选项有:
:
 
 
 
Specifies the domain of the messages files. Supported options are:
 
  
* <code>django</code> for all <code>*.py</code>, <code>*.html</code> and <code>*.txt</code> files (default)
+
* <code>django</code> 适用于所有 <code>*.py</code><code>*.html</code> <code>*.txt</code> 文件(默认)
* <code>djangojs</code> for <code>*.js</code> files
+
* <code>djangojs</code> 用于 <code>*.js</code> 文件
  
; <span id="cmdoption-makemessages-s"></span><code>--symlinks</code><code></code><code>, </code><code>-s</code><code></code>
+
在寻找新的翻译字符串时,跟踪指向目录的符号链接。
:
 
 
 
Follows symlinks to directories when looking for new translation strings.
 
  
 
用法示例:
 
用法示例:
第924行: 第609行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin makemessages --locale=de --symlinks</pre>
+
<syntaxhighlight lang="python">django-admin makemessages --locale=de --symlinks</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <span id="cmdoption-makemessages-i"></span><code>--ignore</code><code> PATTERN</code><code>, </code><code>-i</code><code> PATTERN</code>
+
忽略与给定 <code>glob</code> 样式模式匹配的文件或目录。 多次使用以忽略更多。
:
 
  
Ignores files or directories matching the given <code>glob</code>-style pattern. Use
+
默认使用这些模式:<code>'CVS'</code><code>'.*'</code><code>'*~'</code><code>'*.pyc'</code>
multiple times to ignore more.
 
 
 
These patterns are used by default: <code>'CVS'</code>, <code>'.*'</code>, <code>'*~'</code>, <code>'*.pyc'</code>.
 
  
 
用法示例:
 
用法示例:
第943行: 第624行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin makemessages --locale=en_US --ignore=apps/* --ignore=secret/*.html</pre>
+
<syntaxhighlight lang="python">django-admin makemessages --locale=en_US --ignore=apps/* --ignore=secret/*.html</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--no-default-ignore</code><code></code>
+
禁用 <code>--ignore</code> 的默认值。
:
 
 
 
Disables the default values of <code>--ignore</code>.
 
 
 
; <code>--no-wrap</code><code></code>
 
:
 
  
Disables breaking long message lines into several lines in language files.
+
禁用将语言文件中的长消息行分成几行。
  
; <code>--no-location</code><code></code>
+
禁止在语言文件中写入“<code>#: filename:line</code>”注释行。 使用此选项会使技术熟练的翻译人员更难理解每条消息的上下文。
:
 
  
Suppresses writing '<code>#: filename:line</code>’ comment lines in language files.
+
控制语言文件中的 <code>#: filename:line</code> 注释行。 如果选项是:
Using this option makes it harder for technically skilled translators to
 
understand each message's context.
 
  
; <code>--add-location</code><code> [{full,file,never}]</code>
+
* <code>full</code>(如果没有给出默认值):行包括文件名和行号。
:
+
* <code>file</code>:行号省略。
 +
* <code>never</code>:行被抑制(同<code>--no-location</code>)。
  
Controls <code>#: filename:line</code> comment lines in language files. If the option
+
需要 <code>gettext</code> 0.19 或更新版本。
is:
 
  
* <code>full</code> (the default if not given): the lines include both file name and line number.
+
防止删除在创建 <code>.po</code> 文件之前生成的临时 <code>.pot</code> 文件。 这对于调试可能阻止创建最终语言文件的错误很有用。
* <code>file</code>: the line number is omitted.
 
* <code>never</code>: the lines are suppressed (same as [[#cmdoption-makemessages-no-location|<code>--no-location</code>]]).
 
 
 
Requires <code>gettext</code> 0.19 or newer.
 
 
 
; <code>--keep-pot</code><code></code>
 
:
 
 
 
Prevents deleting the temporary <code>.pot</code> files generated before creating the
 
<code>.po</code> file. This is useful for debugging errors which may prevent the final
 
language files from being created.
 
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
See [[../../topics/i18n/translation#customizing-makemessages|<span class="std std-ref">Customizing the makemessages command</span>]] for instructions on how to customize
+
有关如何自定义 [[#id43|:djadmin:`makemessages`]] 传递给 <code>xgettext</code> 的关键字的说明,请参阅 [[../../topics/i18n/translation#customizing-makemessages|自定义 makemessages 命令]]
the keywords that [[#django-admin-makemessages|<code>makemessages</code>]] passes to <code>xgettext</code>.
 
  
  
第997行: 第657行:
 
<div id="makemigrations" class="section">
 
<div id="makemigrations" class="section">
  
=== <code>makemigrations</code> ===
+
=== makemigrations ===
  
; <code>django-admin makemigrations [app_label [app_label ...]]</code>
+
根据检测到的模型更改创建新迁移。 [[../../topics/migrations|迁移文档]] 中详细介绍了迁移及其与应用程序的关系等。
:
 
  
Creates new migrations based on the changes detected to your models.
+
提供一个或多个应用程序名称作为参数将限制为指定的应用程序创建的迁移以及所需的任何依赖项(例如 <code>ForeignKey</code> 另一端的表)。
Migrations, their relationship with apps and more are covered in depth in
 
[[../../topics/migrations|<span class="doc">the migrations documentation</span>]].
 
  
Providing one or more app names as arguments will limit the migrations created
+
要将迁移添加到没有 <code>migrations</code> 目录的应用程序,请使用应用程序的 <code>app_label</code> 运行 <code>makemigrations</code>
to the app(s) specified and any dependencies needed (the table at the other end
 
of a <code>ForeignKey</code>, for example).
 
  
To add migrations to an app that doesn't have a <code>migrations</code> directory, run
+
禁止所有用户提示。 如果无法自动解决被抑制的提示,该命令将退出并显示错误代码 3。
<code>makemigrations</code> with the app's <code>app_label</code>.
 
  
; <span id="cmdoption-makemigrations-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
+
输出指定应用程序的空迁移,以进行手动编辑。 这适用于高级用户,除非您熟悉迁移格式、迁移操作以及迁移之间的依赖关系,否则不应使用。
:
 
  
Suppresses all user prompts. If a suppressed prompt cannot be resolved
+
显示将在不实际将任何迁移文件写入磁盘的情况下进行的迁移。 将此选项与 <code>--verbosity 3</code> 一起使用还将显示将写入的完整迁移文件。
automatically, the command will exit with error code 3.
 
  
; <code>--empty</code><code></code>
+
可以解决迁移冲突。
:
 
  
Outputs an empty migration for the specified apps, for manual editing. This is
+
允许命名生成的迁移而不是使用生成的名称。 名称必须是有效的 Python <span class="xref std std-ref"> 标识符 </span>
for advanced users and should not be used unless you are familiar with the
 
migration format, migration operations, and the dependencies between your
 
migrations.
 
 
 
; <code>--dry-run</code><code></code>
 
:
 
 
 
Shows what migrations would be made without actually writing any migrations
 
files to disk. Using this option along with <code>--verbosity 3</code> will also show
 
the complete migrations files that would be written.
 
 
 
; <code>--merge</code><code></code>
 
:
 
 
 
Enables fixing of migration conflicts.
 
 
 
; <span id="cmdoption-makemigrations-n"></span><code>--name</code><code> NAME</code><code>, </code><code>-n</code><code> NAME</code>
 
:
 
 
 
Allows naming the generated migration(s) instead of using a generated name. The
 
name must be a valid Python <span class="xref std std-ref">identifier</span>.
 
 
 
; <code>--no-header</code><code></code>
 
:
 
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
Generate migration files without Django version and timestamp header.
+
生成没有 Django 版本和时间戳头的迁移文件。
 
 
; <code>--check</code><code></code>
 
:
 
  
Makes <code>makemigrations</code> exit with a non-zero status when model changes without
+
当检测到没有迁移的模型更改时,使 <code>makemigrations</code> 以非零状态退出。
migrations are detected.
 
  
  
第1,065行: 第689行:
 
<div id="migrate" class="section">
 
<div id="migrate" class="section">
  
=== <code>migrate</code> ===
+
=== migrate ===
 
 
; <code>django-admin migrate [app_label] [migration_name]</code>
 
:
 
  
Synchronizes the database state with the current set of models and migrations.
+
将数据库状态与当前的模型和迁移集同步。 [[../../topics/migrations|迁移文档]] 中详细介绍了迁移及其与应用程序的关系等。
Migrations, their relationship with apps and more are covered in depth in
 
[[../../topics/migrations|<span class="doc">the migrations documentation</span>]].
 
  
The behavior of this command changes depending on the arguments provided:
+
该命令的行为根据提供的参数而改变:
  
* No arguments: All apps have all of their migrations run.
+
* 没有参数:运行所有的应用程序的所有迁移。
* <code>&lt;app_label&gt;</code>: The specified app has its migrations run, up to the most recent migration. This may involve running other apps' migrations too, due to dependencies.
+
* <code>&lt;app_label&gt;</code>:指定的应用程序运行其迁移,直到最近的迁移。 由于依赖关系,这也可能涉及运行其他应用程序的迁移。
* <code>&lt;app_label&gt; &lt;migrationname&gt;</code>: Brings the database schema to a state where the named migration is applied, but no later migrations in the same app are applied. This may involve unapplying migrations if you have previously migrated past the named migration. You can use a prefix of the migration name, e.g. <code>0001</code>, as long as it's unique for the given app name. Use the name <code>zero</code> to migrate all the way back i.e. to revert all applied migrations for an app.
+
* <code>&lt;app_label&gt; &lt;migrationname&gt;</code>:将数据库架构带到应用命名迁移的状态,但不会应用同一应用程序中的后续迁移。 如果您之前迁移过指定迁移,则这可能涉及取消应用迁移。 您可以使用迁移名称的前缀,例如 <code>0001</code>,只要它对于给定的应用程序名称是唯一的。 使用名称 <code>zero</code> 一路迁移回来即 恢复应用程序的所有应用迁移。
  
 
<div class="admonition warning">
 
<div class="admonition warning">
第1,084行: 第703行:
 
警告
 
警告
  
When unapplying migrations, all dependent migrations will also be
+
取消应用迁移时,所有依赖的迁移也将取消应用,无论 <code>&lt;app_label&gt;</code>。 您可以使用 <code>--plan</code> 来检查哪些迁移将不被应用。
unapplied, regardless of <code>&lt;app_label&gt;</code>. You can use <code>--plan</code> to check
 
which migrations will be unapplied.
 
  
  
 
</div>
 
</div>
; <code>--database</code><code> DATABASE</code>
+
指定要迁移的数据库。 默认为 <code>default</code>
:
 
 
 
Specifies the database to migrate. Defaults to <code>default</code>.
 
  
; <code>--fake</code><code></code>
+
标记目标的迁移(按照上面的规则)已应用,但没有实际运行 SQL 来改变你的数据库架构。
:
 
  
Marks the migrations up to the target one (following the rules above) as
+
这适用于高级用户在手动应用更改时直接操作当前迁移状态; 请注意,使用 <code>--fake</code> 存在将迁移状态表置于需要手动恢复以使迁移正确运行的状态的风险。
applied, but without actually running the SQL to change your database schema.
 
  
This is intended for advanced users to manipulate the
+
如果在该迁移中的所有 [[../migration-operations#django.db.migrations.operations|CreateModel]] 操作创建的具有所有模型名称的所有数据库表已经存在,则允许 Django 跳过应用程序的初始迁移。 此选项适用于首次针对预先存在使用迁移的数据库运行迁移时使用。 但是,此选项不会检查匹配表名称之外的匹配数据库架构,因此只有在您确信现有架构与初始迁移中记录的架构匹配时才可以安全使用。
current migration state directly if they're manually applying changes;
 
be warned that using <code>--fake</code> runs the risk of putting the migration state
 
table into a state where manual recovery will be needed to make migrations
 
run correctly.
 
 
 
; <code>--fake-initial</code><code></code>
 
:
 
 
 
Allows Django to skip an app's initial migration if all database tables with
 
the names of all models created by all
 
[[../migration-operations#django.db.migrations.operations|<code>CreateModel</code>]] operations in that
 
migration already exist. This option is intended for use when first running
 
migrations against a database that preexisted the use of migrations. This
 
option does not, however, check for matching database schema beyond matching
 
table names and so is only safe to use if you are confident that your existing
 
schema matches what is recorded in your initial migration.
 
 
 
; <code>--plan</code><code></code>
 
:
 
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
Shows the migration operations that will be performed for the given <code>migrate</code>
+
显示将为给定的 <code>migrate</code> 命令执行的迁移操作。
command.
 
 
 
; <code>--run-syncdb</code><code></code>
 
:
 
 
 
Allows creating tables for apps without migrations. While this isn't
 
recommended, the migrations framework is sometimes too slow on large projects
 
with hundreds of models.
 
  
; <span id="cmdoption-migrate-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
+
允许为应用程序创建表而无需迁移。 虽然不建议这样做,但迁移框架有时在具有数百个模型的大型项目上太慢。
:
 
  
Suppresses all user prompts. An example prompt is asking about removing stale
+
禁止所有用户提示。 一个示例提示是询问删除陈旧的内容类型。
content types.
 
  
  
第1,147行: 第731行:
 
<div id="runserver" class="section">
 
<div id="runserver" class="section">
  
=== <code>runserver</code> ===
+
=== runserver ===
  
; <code>django-admin runserver [addrport]</code>
+
在本地机器上启动轻量级开发 Web 服务器。 默认情况下,服务器在 IP 地址 <code>127.0.0.1</code> 上的端口 8000 上运行。 您可以明确传入 IP 地址和端口号。
:
 
  
Starts a lightweight development Web server on the local machine. By default,
+
如果您以具有普通权限(推荐)的用户身份运行此脚本,您可能无权在低端口号上启动端口。 为超级用户 (root) 保留低端口号。
the server runs on port 8000 on the IP address <code>127.0.0.1</code>. You can pass in an
 
IP address and port number explicitly.
 
  
If you run this script as a user with normal privileges (recommended), you
+
此服务器使用由 [[#id45|:setting:`WSGI_APPLICATION`]] 设置指定的 WSGI 应用程序对象。
might not have access to start a port on a low port number. Low port numbers
 
are reserved for the superuser (root).
 
  
This server uses the WSGI application object specified by the
+
请勿在生产环境中使用此服务器。 它没有经过安全审计或性能测试。 (这就是它会留下来的方式。 我们的工作是制作 Web 框架,而不是 Web 服务器,因此改进此服务器以处理生产环境超出了 Django 的范围。)
[[../settings#std-setting-WSGI_APPLICATION|<code>WSGI_APPLICATION</code>]] setting.
 
  
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through
+
开发服务器根据需要自动为每个请求重新加载 Python 代码。 您无需重新启动服务器即可使代码更改生效。 但是,某些操作(例如添加文件)不会触发重新启动,因此在这些情况下您必须重新启动服务器。
security audits or performance tests. (And that's how it's gonna stay. We're in
 
the business of making Web frameworks, not Web servers, so improving this
 
server to be able to handle a production environment is outside the scope of
 
Django.)
 
  
The development server automatically reloads Python code for each request, as
+
如果您使用 Linux MacOS 并安装 [https://pypi.org/project/pywatchman/ pywatchman] [https://facebook.github.io/watchman/ Watchman] 服务,内核信号将用于自动重新加载服务器(而不是每秒轮询文件修改时间戳)。 这为大型项目提供了更好的性能、缩短了代码更改后的响应时间、更强大的更改检测并降低了功耗。
needed. You don't need to restart the server for code changes to take effect.
 
However, some actions like adding files don't trigger a restart, so you'll
 
have to restart the server in these cases.
 
 
 
If you're using Linux or MacOS and install both [https://pypi.org/project/pywatchman/ pywatchman] and the
 
[https://facebook.github.io/watchman/ Watchman] service, kernel signals will be used to autoreload the server
 
(rather than polling file modification timestamps each second). This offers
 
better performance on large projects, reduced response time after code changes,
 
more robust change detection, and a reduction in power usage.
 
  
 
<div class="admonition-large-directories-with-many-files-may-cause-performance-issues admonition">
 
<div class="admonition-large-directories-with-many-files-may-cause-performance-issues admonition">
  
Large directories with many files may cause performance issues
+
有许多文件的大目录可能会导致性能问题。
  
When using Watchman with a project that includes large non-Python
+
Watchman 用于包含大型非 Python 目录(如 <code>node_modules</code>)的项目时,建议忽略此目录以获得最佳性能。 有关如何执行此操作的信息,请参阅 [https://facebook.github.io/watchman/docs/config.html#ignore_dirs watchman 文档]
directories like <code>node_modules</code>, it's advisable to ignore this directory
 
for optimal performance. See the [https://facebook.github.io/watchman/docs/config.html#ignore_dirs watchman documentation] for information
 
on how to do this.
 
  
  
第1,193行: 第755行:
 
<div class="admonition-watchman-timeout admonition">
 
<div class="admonition-watchman-timeout admonition">
  
Watchman timeout
+
Watchman 超时
  
The default timeout of <code>Watchman</code> client is 5 seconds. You can change it
+
<code>Watchman</code> 客户端的默认超时时间为 5 秒。 您可以通过设置 <code>DJANGO_WATCHMAN_TIMEOUT</code> 环境变量来更改它。
by setting the <code>DJANGO_WATCHMAN_TIMEOUT</code> environment variable.
 
  
  
第1,202行: 第763行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
Watchman support replaced support for pyinotify.
+
<span class="versionmodified changed"> 2.2 版更改:</span>Watchman 支持取代了对 pyinotify 的支持。
  
  
 
</div>
 
</div>
When you start the server, and each time you change Python code while the
+
当您启动服务器时,每次在服务器运行时更改 Python 代码时,系统检查框架都会检查您的整个 Django 项目是否存在一些常见错误(请参阅 [[#id47|:djadmin:`check`]] 命令) . 如果发现任何错误,它们将打印到标准输出。
server is running, the system check framework will check your entire Django
 
project for some common errors (see the [[#django-admin-check|<code>check</code>]] command). If any
 
errors are found, they will be printed to standard output.
 
  
You can run as many concurrent servers as you want, as long as they're on
+
您可以根据需要运行任意数量的并发服务器,只要它们位于不同的端口上即可。 只需多次执行 <code>django-admin runserver</code>
separate ports. Just execute <code>django-admin runserver</code> more than once.
 
  
Note that the default IP address, <code>127.0.0.1</code>, is not accessible from other
+
请注意,网络上的其他机器无法访问默认 IP 地址 <code>127.0.0.1</code>。 要使网络上的其他机器可以查看您的开发服务器,请使用其自己的 IP 地址(例如 <code>192.168.2.1</code>) <code>0.0.0.0</code> <code>::</code>(启用 IPv6)。
machines on your network. To make your development server viewable to other
 
machines on the network, use its own IP address (e.g. <code>192.168.2.1</code>) or
 
<code>0.0.0.0</code> or <code>::</code> (with IPv6 enabled).
 
  
You can provide an IPv6 address surrounded by brackets
+
您可以提供一个用括号括起来的 IPv6 地址(例如 <code>[200a::1]:8000</code>)。 这将自动启用 IPv6 支持。
(e.g. <code>[200a::1]:8000</code>). This will automatically enable IPv6 support.
 
  
A hostname containing ASCII-only characters can also be used.
+
也可以使用只包含 ASCII 字符的主机名。
  
If the [[../contrib/staticfiles|<span class="doc">staticfiles</span>]] contrib app is enabled
+
如果启用了 [[../contrib/staticfiles|staticfiles]] contrib 应用程序(新项目中的默认设置),则 [[#id49|:djadmin:`runserver`]] 命令将被其自己的 [[../contrib/staticfiles#staticfiles-runserver|runserver]] 命令覆盖。
(default in new projects) the [[#django-admin-runserver|<code>runserver</code>]] command will be overridden
 
with its own [[../contrib/staticfiles#staticfiles-runserver|<span class="std std-ref">runserver</span>]] command.
 
  
Logging of each request and response of the server is sent to the
+
服务器的每个请求和响应的日志发送到 [[../../topics/logging#django-server-logger|django.server]] 记录器。
[[../../topics/logging#django-server-logger|<span class="std std-ref">django.server</span>]] logger.
 
  
; <code>--noreload</code><code></code>
+
禁用自动重新加载器。 这意味着您在服务器运行时所做的任何 Python 代码更改将 ''不会'' 生效,如果特定的 Python 模块已经加载到内存中。
:
 
  
Disables the auto-reloader. This means any Python code changes you make while
+
在开发服务器中禁用线程。 服务器默认是多线程的。
the server is running will ''not'' take effect if the particular Python modules
 
have already been loaded into memory.
 
  
; <code>--nothreading</code><code></code>
+
对开发服务器使用 IPv6。 这会将默认 IP 地址从 <code>127.0.0.1</code> 更改为 <code>::1</code>
:
 
 
 
Disables use of threading in the development server. The server is
 
multithreaded by default.
 
 
 
; <span id="cmdoption-runserver-6"></span><code>--ipv6</code><code></code><code>, </code><code>-6</code><code></code>
 
:
 
 
 
Uses IPv6 for the development server. This changes the default IP address from
 
<code>127.0.0.1</code> to <code>::1</code>.
 
  
 
<div id="examples-of-using-different-ports-and-addresses" class="section">
 
<div id="examples-of-using-different-ports-and-addresses" class="section">
  
==== Examples of using different ports and addresses ====
+
==== 使用不同端口和地址的例子 ====
  
Port 8000 on IP address <code>127.0.0.1</code>:
+
IP 地址 <code>127.0.0.1</code> 上的端口 8000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,260行: 第797行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver</pre>
+
<syntaxhighlight lang="python">django-admin runserver</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 8000 on IP address <code>1.2.3.4</code>:
+
IP 地址 <code>1.2.3.4</code> 上的端口 8000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,271行: 第808行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver 1.2.3.4:8000</pre>
+
<syntaxhighlight lang="python">django-admin runserver 1.2.3.4:8000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 7000 on IP address <code>127.0.0.1</code>:
+
IP 地址 <code>127.0.0.1</code> 上的端口 7000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,282行: 第819行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver 7000</pre>
+
<syntaxhighlight lang="python">django-admin runserver 7000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 7000 on IP address <code>1.2.3.4</code>:
+
IP 地址 <code>1.2.3.4</code> 上的端口 7000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,293行: 第830行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver 1.2.3.4:7000</pre>
+
<syntaxhighlight lang="python">django-admin runserver 1.2.3.4:7000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 8000 on IPv6 address <code>::1</code>:
+
IPv6 地址 <code>::1</code> 上的端口 8000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,304行: 第841行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver -6</pre>
+
<syntaxhighlight lang="python">django-admin runserver -6</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 7000 on IPv6 address <code>::1</code>:
+
IPv6 地址 <code>::1</code> 上的端口 7000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,315行: 第852行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver -6 7000</pre>
+
<syntaxhighlight lang="python">django-admin runserver -6 7000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 7000 on IPv6 address <code>2001:0db8:1234:5678::9</code>:
+
IPv6 地址 <code>2001:0db8:1234:5678::9</code> 上的端口 7000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,326行: 第863行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver [2001:0db8:1234:5678::9]:7000</pre>
+
<syntaxhighlight lang="python">django-admin runserver [2001:0db8:1234:5678::9]:7000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 8000 on IPv4 address of host <code>localhost</code>:
+
主机 <code>localhost</code> 的 IPv4 地址上的端口 8000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,337行: 第874行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver localhost:8000</pre>
+
<syntaxhighlight lang="python">django-admin runserver localhost:8000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Port 8000 on IPv6 address of host <code>localhost</code>:
+
主机 <code>localhost</code> 的 IPv6 地址上的端口 8000:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,348行: 第885行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver -6 localhost:8000</pre>
+
<syntaxhighlight lang="python">django-admin runserver -6 localhost:8000</syntaxhighlight>
  
 
</div>
 
</div>
第1,357行: 第894行:
 
<div id="serving-static-files-with-the-development-server" class="section">
 
<div id="serving-static-files-with-the-development-server" class="section">
  
==== Serving static files with the development server ====
+
==== 用开发服务器服务静态文件 ====
  
By default, the development server doesn't serve any static files for your site
+
默认情况下,开发服务器不会为您的站点提供任何静态文件(例如 CSS 文件、图像、[[#id51|:setting:`MEDIA_URL`]] 下的内容等)。 如果要配置 Django 以提供静态媒体服务,请阅读管理静态文件(例如 图片、JavaScript、CSS) .
(such as CSS files, images, things under [[../settings#std-setting-MEDIA_URL|<code>MEDIA_URL</code>]] and so forth). If
 
you want to configure Django to serve static media, read
 
[[../../howto/static-files/index|<span class="doc">管理静态文件(比如图片、JavaScript、CSS)</span>]].
 
  
  
第1,370行: 第904行:
 
<div id="sendtestemail" class="section">
 
<div id="sendtestemail" class="section">
  
=== <code>sendtestemail</code> ===
+
=== sendtestemail ===
  
; <code>django-admin sendtestemail [email [email ...]]</code>
+
向指定的收件人发送测试电子邮件(以确认通过 Django 发送的电子邮件正在工作)。 例如:
:
 
 
 
Sends a test email (to confirm email sending through Django is working) to the
 
recipient(s) specified. For example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,382行: 第912行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin sendtestemail foo@example.com bar@example.com</pre>
+
<syntaxhighlight lang="python">django-admin sendtestemail foo@example.com bar@example.com</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
There are a couple of options, and you may use any combination of them
+
有几个选项,你可以将它们任意组合在一起使用:
together:
 
  
; <code>--managers</code><code></code>
+
使用 [[../../topics/email#django.core.mail|mail_managers()]] 邮寄 [[#id53|:setting:`MANAGERS`]] 中指定的电子邮件地址。
:  
 
  
Mails the email addresses specified in [[../settings#std-setting-MANAGERS|<code>MANAGERS</code>]] using
+
使用 [[../../topics/email#django.core.mail|mail_admins()]] 邮寄 [[#id55|:setting:`ADMINS`]] 中指定的电子邮件地址。
[[../../topics/email#django.core.mail|<code>mail_managers()</code>]].
 
 
 
; <code>--admins</code><code></code>
 
:
 
 
 
Mails the email addresses specified in [[../settings#std-setting-ADMINS|<code>ADMINS</code>]] using
 
[[../../topics/email#django.core.mail|<code>mail_admins()</code>]].
 
  
  
第1,406行: 第927行:
 
<div id="shell" class="section">
 
<div id="shell" class="section">
  
=== <code>shell</code> ===
+
=== shell ===
 
 
; <code>django-admin shell</code>
 
:
 
  
Starts the Python interactive interpreter.
+
启动 Python 交互式解释器。
  
; <span id="cmdoption-shell-i"></span><code>--interface</code><code> {ipython,bpython,python}</code><code>, </code><code>-i</code><code> {ipython,bpython,python}</code>
+
指定要使用的外壳。 默认情况下,如果安装了 [https://ipython.org/ IPython] 或 [https://bpython-interpreter.org/ bpython],Django 将使用。 如果两者都安装了,请指定您想要的那个:
:
 
  
Specifies the shell to use. By default, Django will use [https://ipython.org/ IPython] or [https://bpython-interpreter.org/ bpython] if
+
蟒蛇:
either is installed. If both are installed, specify which one you want like so:
 
 
 
IPython:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,425行: 第939行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin shell -i ipython</pre>
+
<syntaxhighlight lang="python">django-admin shell -i ipython</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
bpython:
+
蟒蛇:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,436行: 第950行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin shell -i bpython</pre>
+
<syntaxhighlight lang="python">django-admin shell -i bpython</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
If you have a &quot;rich&quot; shell installed but want to force use of the &quot;plain&quot;
+
如果您安装了“丰富的”shell,但想强制使用“普通”的 Python 解释器,请使用 <code>python</code> 作为接口名称,如下所示:
Python interpreter, use <code>python</code> as the interface name, like so:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,448行: 第961行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin shell -i python</pre>
+
<syntaxhighlight lang="python">django-admin shell -i python</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--nostartup</code><code></code>
+
禁止读取“普通”Python 解释器的启动脚本。 默认情况下,读取 <span id="index-3" class="target"></span><code>PYTHONSTARTUP</code> 环境变量或 <code>~/.pythonrc.py</code> 脚本指向的脚本。
:
 
 
 
Disables reading the startup script for the &quot;plain&quot; Python interpreter. By
 
default, the script pointed to by the <span id="index-3" class="target"></span><code>PYTHONSTARTUP</code> environment
 
variable or the <code>~/.pythonrc.py</code> script is read.
 
 
 
; <span id="cmdoption-shell-c"></span><code>--command</code><code> COMMAND</code><code>, </code><code>-c</code><code> COMMAND</code>
 
:
 
  
Lets you pass a command as a string to execute it as Django, like so:
+
允许您将命令作为字符串传递以作为 Django 执行它,如下所示:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,469行: 第974行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin shell --command=&quot;import django; print(django.__version__)&quot;</pre>
+
<syntaxhighlight lang="python">django-admin shell --command="import django; print(django.__version__)"</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
You can also pass code in on standard input to execute it. For example:
+
您还可以在标准输入上传递代码以执行它。 例如:
  
 
<div class="highlight-console notranslate">
 
<div class="highlight-console notranslate">
第1,480行: 第985行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>$ django-admin shell &lt;&lt;EOF
+
<syntaxhighlight lang="console">$ django-admin shell <<EOF
&gt; import django
+
> import django
&gt; print(django.__version__)
+
> print(django.__version__)
&gt; EOF</pre>
+
> EOF</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
On Windows, the REPL is output due to implementation limits of
+
Windows 上,由于该平台上 <code>select.select()</code> 的实现限制,会输出 REPL。
<code>select.select()</code> on that platform.
 
  
  
第1,495行: 第999行:
 
<div id="showmigrations" class="section">
 
<div id="showmigrations" class="section">
  
=== <code>showmigrations</code> ===
+
=== showmigrations ===
 
 
; <code>django-admin showmigrations [app_label [app_label ...]]</code>
 
:
 
 
 
Shows all migrations in a project. You can choose from one of two formats:
 
  
; <span id="cmdoption-showmigrations-l"></span><code>--list</code><code></code><code>, </code><code>-l</code><code></code>
+
显示项目中的所有迁移。 您可以选择以下两种格式之一:
:
 
  
Lists all of the apps Django knows about, the migrations available for each
+
列出 Django 知道的所有应用程序、每个应用程序可用的迁移以及是否应用了每个迁移(由迁移名称旁边的 <code>[X]</code> 标记)。
app, and whether or not each migration is applied (marked by an <code>[X]</code> next to
 
the migration name).
 
  
Apps without migrations are also listed, but have <code>(no migrations)</code> printed
+
还列出了没有迁移的应用程序,但在它们下面印有 <code>(no migrations)</code>
under them.
 
  
This is the default output format.
+
这是默认的输出格式。
  
; <span id="cmdoption-showmigrations-p"></span><code>--plan</code><code></code><code>, </code><code>-p</code><code></code>
+
显示 Django 将遵循的迁移计划来应用迁移。 与 <code>--list</code> 一样,已应用的迁移由 <code>[X]</code> 标记。 对于 2 及以上的 <code>--verbosity</code>,还将显示迁移的所有依赖项。
:
 
  
Shows the migration plan Django will follow to apply migrations. Like
+
<code>app_label</code> 的参数限制了输出,但是,也可能包含提供的应用程序的依赖项。
<code>--list</code>, applied migrations are marked by an <code>[X]</code>. For a <code>--verbosity</code>
 
of 2 and above, all dependencies of a migration will also be shown.
 
  
<code>app_label</code>s arguments limit the output, however, dependencies of provided
+
指定要检查的数据库。 默认为 <code>default</code>
apps may also be included.
 
 
 
; <code>--database</code><code> DATABASE</code>
 
:
 
 
 
Specifies the database to examine. Defaults to <code>default</code>.
 
  
  
第1,533行: 第1,019行:
 
<div id="sqlflush" class="section">
 
<div id="sqlflush" class="section">
  
=== <code>sqlflush</code> ===
+
=== sqlflush ===
  
; <code>django-admin sqlflush</code>
+
打印将为 [[#id57|:djadmin:`flush`]] 命令执行的 SQL 语句。
:
 
 
 
Prints the SQL statements that would be executed for the [[#django-admin-flush|<code>flush</code>]]
 
command.
 
 
 
; <code>--database</code><code> DATABASE</code>
 
:
 
  
Specifies the database for which to print the SQL. Defaults to <code>default</code>.
+
指定要为其打印 SQL 的数据库。 默认为 <code>default</code>
  
  
第1,550行: 第1,029行:
 
<div id="sqlmigrate" class="section">
 
<div id="sqlmigrate" class="section">
  
=== <code>sqlmigrate</code> ===
+
=== sqlmigrate ===
  
; <code>django-admin sqlmigrate app_label migration_name</code>
+
打印指定迁移的 SQL。 这需要一个活动的数据库连接,它将用于解析约束名称; 这意味着您必须针对您希望稍后应用的数据库副本生成 SQL。
:
 
  
Prints the SQL for the named migration. This requires an active database
+
请注意, <code>sqlmigrate</code> 不会为其输出着色。
connection, which it will use to resolve constraint names; this means you must
 
generate the SQL against a copy of the database you wish to later apply it on.
 
  
Note that <code>sqlmigrate</code> doesn't colorize its output.
+
生成用于取消应用迁移的 SQL。 默认情况下,创建的 SQL 用于运行正向迁移。
  
; <code>--backwards</code><code></code>
+
指定要为其生成 SQL 的数据库。 默认为 <code>default</code>
:
 
 
 
Generates the SQL for unapplying the migration. By default, the SQL created is
 
for running the migration in the forwards direction.
 
 
 
; <code>--database</code><code> DATABASE</code>
 
:
 
 
 
Specifies the database for which to generate the SQL. Defaults to <code>default</code>.
 
  
  
第1,576行: 第1,043行:
 
<div id="sqlsequencereset" class="section">
 
<div id="sqlsequencereset" class="section">
  
=== <code>sqlsequencereset</code> ===
+
=== sqlsequencereset ===
 
 
; <code>django-admin sqlsequencereset app_label [app_label ...]</code>
 
:
 
  
Prints the SQL statements for resetting sequences for the given app name(s).
+
打印用于重置给定应用名称序列的 SQL 语句。
  
Sequences are indexes used by some database engines to track the next available
+
序列是一些数据库引擎用来跟踪自动递增字段的下一个可用数字的索引。
number for automatically incremented fields.
 
  
Use this command to generate SQL which will fix cases where a sequence is out
+
使用此命令生成 SQL,它将修复序列与其自动递增的字段数据不同步的情况。
of sync with its automatically incremented field data.
 
  
; <code>--database</code><code> DATABASE</code>
+
指定要为其打印 SQL 的数据库。 默认为 <code>default</code>
:
 
 
 
Specifies the database for which to print the SQL. Defaults to <code>default</code>.
 
  
  
第1,598行: 第1,057行:
 
<div id="squashmigrations" class="section">
 
<div id="squashmigrations" class="section">
  
=== <code>squashmigrations</code> ===
+
=== squashmigrations ===
  
; <code>django-admin squashmigrations app_label [start_migration_name] migration_name</code>
+
如果可能,将 <code>app_label</code> 上至并包括 <code>migration_name</code> 的迁移压缩为更少的迁移。 由此产生的被压扁的迁移可以安全地与未被压扁的迁移一起生活。 有关更多信息,请阅读 [[../../topics/migrations#migration-squashing|压缩迁移]] 。
:
 
  
Squashes the migrations for <code>app_label</code> up to and including <code>migration_name</code>
+
当给出 <code>start_migration_name</code> 时,Django 将只包含从此迁移开始并包含此迁移的迁移。 这有助于减轻 [[../migration-operations#django.db.migrations.operations|RunPython]] 和 [[../migration-operations#django.db.migrations.operations|django.db.migrations.operations.RunSQL]] 迁移操作的压缩限制。
down into fewer migrations, if possible. The resulting squashed migrations
 
can live alongside the unsquashed ones safely. For more information,
 
please read [[../../topics/migrations#migration-squashing|<span class="std std-ref">Squashing migrations</span>]].
 
  
When <code>start_migration_name</code> is given, Django will only include migrations
+
在生成压缩迁移时禁用优化器。 默认情况下,Django 会尝试优化迁移中的操作以减小结果文件的大小。 如果此过程失败或创建不正确的迁移,请使用此选项,但也请提交有关该行为的 Django 错误报告,因为优化是安全的。
starting from and including this migration. This helps to mitigate the
 
squashing limitation of [[../migration-operations#django.db.migrations.operations|<code>RunPython</code>]] and
 
[[../migration-operations#django.db.migrations.operations|<code>django.db.migrations.operations.RunSQL</code>]] migration operations.
 
  
; <code>--no-optimize</code><code></code>
+
禁止所有的用户提示。
:
 
  
Disables the optimizer when generating a squashed migration. By default, Django
+
设置压缩迁移的名称。 省略时,名称基于第一次和最后一次迁移,中间有 <code>_squashed_</code>
will try to optimize the operations in your migrations to reduce the size of
 
the resulting file. Use this option if this process is failing or creating
 
incorrect migrations, though please also file a Django bug report about the
 
behavior, as optimization is meant to be safe.
 
 
 
; <span id="cmdoption-squashmigrations-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
 
:
 
 
 
Suppresses all user prompts.
 
 
 
; <code>--squashed-name</code><code> SQUASHED_NAME</code>
 
:
 
 
 
Sets the name of the squashed migration. When omitted, the name is based on the
 
first and last migration, with <code>_squashed_</code> in between.
 
 
 
; <code>--no-header</code><code></code>
 
:
 
  
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
Generate squashed migration file without Django version and timestamp header.
+
生成没有 Django 版本和时间戳头的压缩迁移文件。
  
  
第1,647行: 第1,081行:
 
<div id="startapp" class="section">
 
<div id="startapp" class="section">
  
=== <code>startapp</code> ===
+
=== startapp ===
 
 
; <code>django-admin startapp name [directory]</code>
 
:
 
  
Creates a Django app directory structure for the given app name in the current
+
在当前目录或给定的目标目录中为给定的应用名创建一个 Django 应用目录结构。
directory or the given destination.
 
  
By default, [https://github.com/django/django/blob/stable/2.2.x/django/conf/app_template the new directory] contains a
+
默认情况下, [[#id59|:source:`新目录 `]] 包含一个<code>models.py</code>文件和其他应用程序模板文件。 如果只给出了应用程序名称,应用程序目录将在当前工作目录中创建。
<code>models.py</code> file and other app template files. If only the app name is given,
 
the app directory will be created in the current working directory.
 
  
If the optional destination is provided, Django will use that existing
+
如果提供了可选目标,Django 将使用现有目录而不是创建一个新目录。 您可以使用 '' 表示当前工作目录。
directory rather than creating a new one. You can use '.' to denote the current
 
working directory.
 
  
例子:
+
例如:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,669行: 第1,095行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin startapp myapp /Users/jezdez/Code/myapp</pre>
+
<syntaxhighlight lang="python">django-admin startapp myapp /Users/jezdez/Code/myapp</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<span id="custom-app-and-project-templates" class="target"></span>
+
提供自定义应用程序模板文件的目录路径或压缩文件的路径(<code>.tar.gz</code><code>.tar.bz2</code><code>.tgz</code><code>.tbz</code><code>.zip</code>) 包含应用程序模板文件。
; <code>--template</code><code> TEMPLATE</code>
 
:
 
 
 
Provides the path to a directory with a custom app template file or a path to a
 
compressed file (<code>.tar.gz</code>, <code>.tar.bz2</code>, <code>.tgz</code>, <code>.tbz</code>, <code>.zip</code>)
 
containing the app template files.
 
  
For example, this would look for an app template in the given directory when
+
例如,这将在创建 <code>myapp</code> 应用程序时在给定目录中查找应用程序模板:
creating the <code>myapp</code> app:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,689行: 第1,108行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin startapp --template=/Users/jezdez/Code/my_app_template myapp</pre>
+
<syntaxhighlight lang="python">django-admin startapp --template=/Users/jezdez/Code/my_app_template myapp</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Django will also accept URLs (<code>http</code>, <code>https</code>, <code>ftp</code>) to compressed
+
Django 还将接受 URL(<code>http</code><code>https</code><code>ftp</code>)到带有应用程序模板文件的压缩档案,即时下载和提取它们。
archives with the app template files, downloading and extracting them on the
 
fly.
 
  
For example, taking advantage of GitHub's feature to expose repositories as
+
例如,利用 GitHub 的功能将存储库公开为 zip 文件,您可以使用如下 URL:
zip files, you can use a URL like:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,705行: 第1,121行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin startapp --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp</pre>
+
<syntaxhighlight lang="python">django-admin startapp --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <span id="cmdoption-startapp-e"></span><code>--extension</code><code> EXTENSIONS</code><code>, </code><code>-e</code><code> EXTENSIONS</code>
+
指定应使用模板引擎呈现应用模板中的哪些文件扩展名。 默认为 <code>py</code>
:
 
 
 
Specifies which file extensions in the app template should be rendered with the
 
template engine. Defaults to <code>py</code>.
 
 
 
; <span id="cmdoption-startapp-n"></span><code>--name</code><code> FILES</code><code>, </code><code>-n</code><code> FILES</code>
 
:
 
  
Specifies which files in the app template (in addition to those matching
+
指定应使用模板引擎呈现应用模板中的哪些文件(除了那些匹配的 <code>--extension</code>)。 默认为空列表。
<code>--extension</code>) should be rendered with the template engine. Defaults to an
 
empty list.
 
  
The [[../templates/api#django.template|<code>template context</code>]] used for all matching
+
用于所有匹配文件的 [[../templates/api#django.template|模板上下文]] 是:
files is:
 
  
* Any option passed to the <code>startapp</code> command (among the command's supported options)
+
* 传递给 <code>startapp</code> 命令的任何选项(在命令支持的选项中)
* <code>app_name</code> -- the app name as passed to the command
+
* <code>app_name</code> – 传递给命令的应用程序名称
* <code>app_directory</code> -- the full path of the newly created app
+
* <code>app_directory</code> – 新创建的应用程序的完整路径
* <code>camel_case_app_name</code> -- the app name in camel case format
+
* <code>camel_case_app_name</code> – 驼峰格式的应用名称
* <code>docs_version</code> -- the version of the documentation: <code>'dev'</code> or <code>'1.x'</code>
+
* <code>docs_version</code> – 文档版本:<code>'dev'</code> <code>'1.x'</code>
* <code>django_version</code> -- the version of Django, e.g. <code>'2.0.3'</code>
+
* <code>django_version</code> Django 的版本,例如 <code>'2.0.3'</code>
  
 
<div id="render-warning" class="admonition warning">
 
<div id="render-warning" class="admonition warning">
第1,737行: 第1,143行:
 
警告
 
警告
  
When the app template files are rendered with the Django template
+
当应用模板文件使用 Django 模板引擎(默认所有 <code>*.py</code> 文件)呈现时,Django 也会替换包含的所有杂散模板变量。 例如,如果其中一个 Python 文件包含解释与模板渲染相关的特定功能的文档字符串,则可能会导致示例不正确。
engine (by default all <code>*.py</code> files), Django will also replace all
 
stray template variables contained. For example, if one of the Python files
 
contains a docstring explaining a particular feature related
 
to template rendering, it might result in an incorrect example.
 
  
To work around this problem, you can use the [[../templates/builtins#std-templatetag-templatetag|<code>templatetag</code>]]
+
要解决此问题,您可以使用 [[#id61|:ttag:`templatetag`]] 模板标签来“转义”模板语法的各个部分。
template tag to &quot;escape&quot; the various parts of the template syntax.
 
  
In addition, to allow Python template files that contain Django template
+
此外,为了允许包含 Django 模板语言语法的 Python 模板文件同时防止打包系统尝试字节编译无效的 <code>*.py</code> 文件,以 <code>.py-tpl</code> 结尾的模板文件将重命名为 <code>.py</code>
language syntax while also preventing packaging systems from trying to
 
byte-compile invalid <code>*.py</code> files, template files ending with <code>.py-tpl</code>
 
will be renamed to <code>.py</code>.
 
  
  
第1,757行: 第1,155行:
 
<div id="startproject" class="section">
 
<div id="startproject" class="section">
  
=== <code>startproject</code> ===
+
=== startproject ===
  
; <code>django-admin startproject name [directory]</code>
+
在当前目录或给定的目标目录中为给定的项目名称创建一个 Django 项目目录结构。
:
 
  
Creates a Django project directory structure for the given project name in
+
默认情况下, [[#id63|:source:`新目录 `]] 包含<code>manage.py</code>和一个项目包(包含一个<code>settings.py</code>和其他文件)。
the current directory or the given destination.
 
  
By default, [https://github.com/django/django/blob/stable/2.2.x/django/conf/project_template the new directory] contains
+
如果只给出项目名称,项目目录和项目包都将命名为<code>&lt;projectname&gt;</code>,项目目录将在当前工作目录中创建。
<code>manage.py</code> and a project package (containing a <code>settings.py</code> and other
 
files).
 
  
If only the project name is given, both the project directory and project
+
如果提供了可选目标,Django 将使用该现有目录作为项目目录,并在其中创建 <code>manage.py</code> 和项目包。 用 '。' 表示当前工作目录。
package will be named <code>&lt;projectname&gt;</code> and the project directory
 
will be created in the current working directory.
 
  
If the optional destination is provided, Django will use that existing
+
例如:
directory as the project directory, and create <code>manage.py</code> and the project
 
package within it. Use '.' to denote the current working directory.
 
 
 
例子:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,783行: 第1,171行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin startproject myproject /Users/jezdez/Code/myproject_repo</pre>
+
<syntaxhighlight lang="python">django-admin startproject myproject /Users/jezdez/Code/myproject_repo</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--template</code><code> TEMPLATE</code>
+
指定自定义项目模板的目录、文件路径或 URL。 有关示例和用法,请参阅 <code>startapp --template</code> 文档。
:
 
 
 
Specifies a directory, file path, or URL of a custom project template. See the
 
[[#cmdoption-startapp-template|<code>startapp --template</code>]] documentation for examples and usage.
 
 
 
; <span id="cmdoption-startproject-e"></span><code>--extension</code><code> EXTENSIONS</code><code>, </code><code>-e</code><code> EXTENSIONS</code>
 
:
 
 
 
Specifies which file extensions in the project template should be rendered with
 
the template engine. Defaults to <code>py</code>.
 
  
; <span id="cmdoption-startproject-n"></span><code>--name</code><code> FILES</code><code>, </code><code>-n</code><code> FILES</code>
+
指定应使用模板引擎呈现项目模板中的哪些文件扩展名。 默认为 <code>py</code>
:
 
  
Specifies which files in the project template (in addition to those matching
+
指定应使用模板引擎呈现项目模板中的哪些文件(除了那些匹配的 <code>--extension</code>)。 默认为空列表。
<code>--extension</code>) should be rendered with the template engine. Defaults to an
 
empty list.
 
  
The [[../templates/api#django.template|<code>template context</code>]] used is:
+
使用的 [[../templates/api#django.template|模板上下文]] 是:
  
* Any option passed to the <code>startproject</code> command (among the command's supported options)
+
* 传递给 <code>startproject</code> 命令的任何选项(在命令支持的选项中)
* <code>project_name</code> -- the project name as passed to the command
+
* <code>project_name</code> – 传递给命令的项目名称
* <code>project_directory</code> -- the full path of the newly created project
+
* <code>project_directory</code> – 新建项目的完整路径
* <code>secret_key</code> -- a random key for the [[../settings#std-setting-SECRET_KEY|<code>SECRET_KEY</code>]] setting
+
* <code>secret_key</code> [[#id65|:setting:`SECRET_KEY`]] 设置的随机密钥
* <code>docs_version</code> -- the version of the documentation: <code>'dev'</code> or <code>'1.x'</code>
+
* <code>docs_version</code> – 文档版本:<code>'dev'</code> <code>'1.x'</code>
* <code>django_version</code> -- the version of Django, e.g. <code>'2.0.3'</code>
+
* <code>django_version</code> Django 的版本,例如 <code>'2.0.3'</code>
  
Please also see the [[#render-warning|<span class="std std-ref">rendering warning</span>]] as mentioned
+
另请参阅 [[#id67|:djadmin:`startapp`]] 中提到的 [[#render-warning|渲染警告]]
for [[#django-admin-startapp|<code>startapp</code>]].
 
  
  
第1,823行: 第1,197行:
 
<div id="test" class="section">
 
<div id="test" class="section">
  
=== <code>test</code> ===
+
=== test ===
  
; <code>django-admin test [test_label [test_label ...]]</code>
+
对所有已安装的应用程序运行测试。 有关更多信息,请参阅 [[../../topics/testing/index|在 Django 中进行测试]]
:
 
  
Runs tests for all installed apps. See [[../../topics/testing/index|<span class="doc">Django 中的测试</span>]] for more
+
测试失败后立即停止运行测试并报告失败。
information.
 
  
; <code>--failfast</code><code></code>
+
控制用于执行测试的测试运行器类。 此值覆盖 [[#id69|:setting:`TEST_RUNNER`]] 设置提供的值。
:  
 
  
Stops running tests and reports the failure immediately after a test fails.
+
禁止所有用户提示。 典型的提示是关于删除现有测试数据库的警告。
 
 
; <code>--testrunner</code><code> TESTRUNNER</code>
 
:
 
 
 
Controls the test runner class that is used to execute tests. This value
 
overrides the value provided by the [[../settings#std-setting-TEST_RUNNER|<code>TEST_RUNNER</code>]] setting.
 
 
 
; <span id="cmdoption-test-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
 
:
 
 
 
Suppresses all user prompts. A typical prompt is a warning about deleting an
 
existing test database.
 
  
 
<div id="test-runner-options" class="section">
 
<div id="test-runner-options" class="section">
  
==== Test runner options ====
+
==== 测试运行器选项 ====
  
The <code>test</code> command receives options on behalf of the specified
+
<code>test</code> 命令代表指定的 <code>--testrunner</code> 接收选项。 这些是默认测试运行器的选项:[[../../topics/testing/advanced#django.test.runner|DiscoverRunner]]
[[#cmdoption-test-testrunner|<code>--testrunner</code>]]. These are the options of the default test runner:
 
[[../../topics/testing/advanced#django.test.runner|<code>DiscoverRunner</code>]].
 
  
; <span id="cmdoption-test-k"></span><code>--keepdb</code><code></code><code>, </code><code>-k</code><code></code>
+
在测试运行之间保留测试数据库。 这具有跳过创建和销毁操作的优点,这可以大大减少运行测试的时间,尤其是在大型测试套件中的测试。 如果测试数据库不存在,它将在第一次运行时创建,然后为每次后续运行保留。 在运行测试套件之前,任何未应用的迁移也将应用于测试数据库。
:
 
  
Preserves the test database between test runs. This has the advantage of
+
以相反的执行顺序对测试用例进行排序。 这可能有助于调试未正确隔离的测试的副作用。 [[../../topics/testing/overview#order-of-tests|使用该选项时,按测试类分组]] 被保留。
skipping both the create and destroy actions which can greatly decrease the
 
time to run tests, especially those in a large test suite. If the test database
 
does not exist, it will be created on the first run and then preserved for each
 
subsequent run. Any unapplied migrations will also be applied to the test
 
database before running the test suite.
 
  
; <span id="cmdoption-test-r"></span><code>--reverse</code><code></code><code>, </code><code>-r</code><code></code>
+
在运行测试之前将 [[#id71|:setting:`DEBUG`]] 设置设置为 <code>True</code>。 这可能有助于对测试失败进行故障排除。
:
 
  
Sorts test cases in the opposite execution order. This may help in debugging
+
为失败的测试启用 [[../../topics/logging#django-db-logger|SQL 日志记录]] 。 如果 <code>--verbosity</code> 为 <code>2</code>,则还会输出通过测试的查询。
the side effects of tests that aren't properly isolated. [[../../topics/testing/overview#order-of-tests|<span class="std std-ref">Grouping by test
 
class</span>]] is preserved when using this option.
 
  
; <code>--debug-mode</code><code></code>
+
在单独的并行进程中运行测试。 由于现代处理器具有多个内核,因此可以显着加快测试运行速度。
:
 
  
Sets the [[../settings#std-setting-DEBUG|<code>DEBUG</code>]] setting to <code>True</code> prior to running tests. This may
+
默认情况下,<code>--parallel</code> 根据 <code>multiprocessing.cpu_count()</code> 每个内核运行一个进程。 您可以通过提供它作为选项的值来调整进程数,例如 <code>--parallel=4</code>,或通过设置 <code>DJANGO_TEST_PROCESSES</code> 环境变量。
help troubleshoot test failures.
 
  
; <span id="cmdoption-test-d"></span><code>--debug-sql</code><code></code><code>, </code><code>-d</code><code></code>
+
Django 将测试用例——<code>unittest.TestCase</code> 子类——分发到子进程。 如果测试用例比配置的进程少,Django 会相应地减少进程数。
:
 
  
Enables [[../../topics/logging#django-db-logger|<span class="std std-ref">SQL logging</span>]] for failing tests. If
+
每个进程都有自己的数据库。 您必须确保不同的测试用例不会访问相同的资源。 例如,接触文件系统的测试用例应该创建一个临时目录供他们自己使用。
<code>--verbosity</code> is <code>2</code>, then queries in passing tests are also output.
 
 
 
; <code>--parallel</code><code> [N]</code>
 
:
 
 
 
Runs tests in separate parallel processes. Since modern processors have
 
multiple cores, this allows running tests significantly faster.
 
 
 
By default <code>--parallel</code> runs one process per core according to
 
<code>multiprocessing.cpu_count()</code>. You can adjust the number of processes
 
either by providing it as the option's value, e.g. <code>--parallel=4</code>, or by
 
setting the <code>DJANGO_TEST_PROCESSES</code> environment variable.
 
 
 
Django distributes test cases — <code>unittest.TestCase</code> subclasses — to
 
subprocesses. If there are fewer test cases than configured processes, Django
 
will reduce the number of processes accordingly.
 
 
 
Each process gets its own database. You must ensure that different test cases
 
don't access the same resources. For instance, test cases that touch the
 
filesystem should create a temporary directory for their own use.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
If you have test classes that cannot be run in parallel, you can use
+
如果您有无法并行运行的测试类,您可以使用 <code>SerializeMixin</code> 顺序运行它们。 请参阅 [[../../topics/testing/advanced#topics-testing-enforce-run-sequentially|强制按顺序运行测试类]]
<code>SerializeMixin</code> to run them sequentially. See [[../../topics/testing/advanced#topics-testing-enforce-run-sequentially|<span class="std std-ref">Enforce running test
 
classes sequentially</span>]].
 
  
  
 
</div>
 
</div>
This option requires the third-party <code>tblib</code> package to display tracebacks
+
此选项需要第三方 <code>tblib</code> 包才能正确显示回溯:
correctly:
 
  
 
<div class="highlight-console notranslate">
 
<div class="highlight-console notranslate">
第1,921行: 第1,243行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>$ pip install tblib</pre>
+
<syntaxhighlight lang="console">$ pip install tblib</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
This feature isn't available on Windows. It doesn't work with the Oracle
+
此功能在 Windows 上不可用。 它也不适用于 Oracle 数据库后端。
database backend either.
 
  
If you want to use <code>pdb</code> while debugging tests, you must disable parallel
+
如果要在调试测试时使用 <code>pdb</code>,则必须禁用并行执行 (<code>--parallel=1</code>)。 如果你不这样做,你会看到类似 <code>bdb.BdbQuit</code> 的东西。
execution (<code>--parallel=1</code>). You'll see something like <code>bdb.BdbQuit</code> if you
 
don't.
 
  
 
<div class="admonition warning">
 
<div class="admonition warning">
第1,937行: 第1,256行:
 
警告
 
警告
  
When test parallelization is enabled and a test fails, Django may be
+
当启用测试并行化并且测试失败时,Django 可能无法显示异常回溯。 这会使调试变得困难。 如果遇到此问题,请在没有并行化的情况下运行受影响的测试以查看失败的回溯。
unable to display the exception traceback. This can make debugging
 
difficult. If you encounter this problem, run the affected test without
 
parallelization to see the traceback of the failure.
 
  
This is a known limitation. It arises from the need to serialize objects
+
这是一个已知的限制。 它源于需要序列化对象以便在进程之间交换它们。 详见 <span class="xref std std-ref">python:pickle-picklable</span>
in order to exchange them between processes. See
 
<span class="xref std std-ref">python:pickle-picklable</span> for details.
 
  
  
 
</div>
 
</div>
; <code>--tag</code><code> TAGS</code>
+
; <span class="sig-name descname"><span class="pre">--tag</span></span><span class="sig-prename descclassname"> <span class="pre">TAGS</span></span>
 
:  
 
:  
  
Runs only tests [[../../topics/testing/tools#topics-tagging-tests|<span class="std std-ref">marked with the specified tags</span>]].
+
仅运行标有指定标签 [[../../topics/testing/tools#topics-tagging-tests|的测试]] 。 可以多次指定并与 <code>test --exclude-tag</code> 结合使用。
May be specified multiple times and combined with [[#cmdoption-test-exclude-tag|<code>test --exclude-tag</code>]].
 
  
; <code>--exclude-tag</code><code> EXCLUDE_TAGS</code>
+
; <span class="sig-name descname"><span class="pre">--exclude-tag</span></span><span class="sig-prename descclassname"> <span class="pre">EXCLUDE_TAGS</span></span>
 
:  
 
:  
  
Excludes tests [[../../topics/testing/tools#topics-tagging-tests|<span class="std std-ref">marked with the specified tags</span>]].
+
排除用指定标签[[../../topics/testing/tools#topics-tagging-tests|标记的测试]]。 可以多次指定并与 <code>test --tag</code> 结合使用。
May be specified multiple times and combined with [[#cmdoption-test-tag|<code>test --tag</code>]].
 
  
  
第1,966行: 第1,278行:
 
<div id="testserver" class="section">
 
<div id="testserver" class="section">
  
=== <code>testserver</code> ===
+
=== testserver ===
 
 
; <code>django-admin testserver [fixture [fixture ...]]</code>
 
:
 
  
Runs a Django development server (as in [[#django-admin-runserver|<code>runserver</code>]]) using data from
+
使用来自给定装置的数据运行 Django 开发服务器(如 [[#id73|:djadmin:`runserver`]])。
the given fixture(s).
 
  
For example, this command:
+
例如,这个命令:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第1,980行: 第1,288行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin testserver mydata.json</pre>
+
<syntaxhighlight lang="python">django-admin testserver mydata.json</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
...would perform the following steps:
+
...将执行以下步骤:
 
 
# Create a test database, as described in [[../../topics/testing/overview#the-test-database|<span class="std std-ref">The test database</span>]].
 
# Populate the test database with fixture data from the given fixtures. (For more on fixtures, see the documentation for [[#django-admin-loaddata|<code>loaddata</code>]] above.)
 
# Runs the Django development server (as in [[#django-admin-runserver|<code>runserver</code>]]), pointed at this newly created test database instead of your production database.
 
  
This is useful in a number of ways:
+
# 创建一个测试数据库,如[[../../topics/testing/overview#the-test-database|测试数据库]]中所述。
 +
# 使用来自给定夹具的夹具数据填充测试数据库。 (有关装置的更多信息,请参阅上面的 [[#id75|:djadmin:`loaddata`]] 文档。)
 +
# 运行 Django 开发服务器(如 [[#id77|:djadmin:`runserver`]]),指向这个新创建的测试数据库而不是你的生产数据库。
  
* When you're writing [[../../topics/testing/overview|<span class="doc">unit tests</span>]] of how your views act with certain fixture data, you can use <code>testserver</code> to interact with the views in a Web browser, manually.
+
这在很多方面都很有用:
* Let's say you're developing your Django application and have a &quot;pristine&quot; copy of a database that you'd like to interact with. You can dump your database to a fixture (using the [[#django-admin-dumpdata|<code>dumpdata</code>]] command, explained above), then use <code>testserver</code> to run your Web application with that data. With this arrangement, you have the flexibility of messing up your data in any way, knowing that whatever data changes you're making are only being made to a test database.
 
  
Note that this server does ''not'' automatically detect changes to your Python
+
* 当您编写 [[../../topics/testing/overview|单元测试]] 以了解您的视图如何处理某些夹具数据时,您可以手动使用 <code>testserver</code> 与 Web 浏览器中的视图交互。
source code (as [[#django-admin-runserver|<code>runserver</code>]] does). It does, however, detect changes to
+
* 假设您正在开发您的 Django 应用程序,并且拥有您想要与之交互的数据库的“原始”副本。 您可以将数据库转储到夹具(使用 [[#id79|:djadmin:`dumpdata`]] 命令,如上所述),然后使用 <code>testserver</code> 使用该数据运行 Web 应用程序。 通过这种安排,您可以灵活地以任何方式弄乱您的数据,因为您知道所做的任何数据更改都只会对测试数据库进行。
templates.
 
  
; <code>--addrport</code><code> ADDRPORT</code>
+
请注意,此服务器 ''不会'' 自动检测 Python 源代码的更改(如 [[#id81|:djadmin:`runserver`]] 所做的那样)。 但是,它确实会检测对模板的更改。
:  
 
  
Specifies a different port, or IP address and port, from the default of
+
指定与默认值 <code>127.0.0.1:8000</code> 不同的端口或 IP 地址和端口。 该值遵循与 [[#id83|:djadmin:`runserver`]] 命令的参数完全相同的格式并提供完全相同的功能。
<code>127.0.0.1:8000</code>. This value follows exactly the same format and serves
 
exactly the same function as the argument to the [[#django-admin-runserver|<code>runserver</code>]] command.
 
  
Examples:
+
实际案例
  
To run the test server on port 7000 with <code>fixture1</code> and <code>fixture2</code>:
+
使用 <code>fixture1</code> <code>fixture2</code> 在端口 7000 上运行测试服务器:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,015行: 第1,316行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin testserver --addrport 7000 fixture1 fixture2
+
<syntaxhighlight lang="python">django-admin testserver --addrport 7000 fixture1 fixture2
django-admin testserver fixture1 fixture2 --addrport 7000</pre>
+
django-admin testserver fixture1 fixture2 --addrport 7000</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
(The above statements are equivalent. We include both of them to demonstrate
+
(以上陈述是等价的。 我们将它们都包括在内,以证明选项是在夹具参数之前还是之后都无关紧要。)
that it doesn't matter whether the options come before or after the fixture
 
arguments.)
 
  
To run on 1.2.3.4:7000 with a <code>test</code> fixture:
+
使用 <code>test</code> 夹具在 1.2.3.4:7000 上运行:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,031行: 第1,330行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin testserver --addrport 1.2.3.4:7000 test</pre>
+
<syntaxhighlight lang="python">django-admin testserver --addrport 1.2.3.4:7000 test</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <span id="cmdoption-testserver-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
+
禁止所有用户提示。 典型的提示是关于删除现有测试数据库的警告。
:
 
 
 
Suppresses all user prompts. A typical prompt is a warning about deleting an
 
existing test database.
 
  
  
第2,048行: 第1,343行:
 
<div id="commands-provided-by-applications" class="section">
 
<div id="commands-provided-by-applications" class="section">
  
== Commands provided by applications ==
+
== 应用程序提供的命令 ==
  
Some commands are only available when the <code>django.contrib</code> application that
+
某些命令仅在<code>django.contrib</code>应用程序[[../../howto/custom-management-commands|工具]]他们已经 [[#id85|:设置:`启用 `]] . 本节按应用对它们进行分组。
[[../../howto/custom-management-commands|<span class="doc">implements</span>]] them has been
 
[[../settings#std-setting-INSTALLED_APPS|<code>enabled</code>]]. This section describes them grouped by
 
their application.
 
  
 
<div id="django-contrib-auth" class="section">
 
<div id="django-contrib-auth" class="section">
  
=== <code>django.contrib.auth</code> ===
+
=== django.contrib.auth ===
  
 
<div id="changepassword" class="section">
 
<div id="changepassword" class="section">
  
==== <code>changepassword</code> ====
+
==== changepassword ====
 
 
; <code>django-admin changepassword [&lt;username&gt;]</code>
 
:
 
  
This command is only available if Django's [[../../topics/auth/index|<span class="doc">authentication system</span>]] (<code>django.contrib.auth</code>) is installed.
+
此命令仅在安装了 Django [[../../topics/auth/index|身份验证系统]] (<code>django.contrib.auth</code>) 时可用。
  
Allows changing a user's password. It prompts you to enter a new password twice
+
允许更改用户的密码。 它会提示您为给定用户输入两次新密码。 如果条目相同,这将立即成为新密码。 如果您不提供用户,该命令将尝试更改用户名与当前用户匹配的密码。
for the given user. If the entries are identical, this immediately becomes the
 
new password. If you do not supply a user, the command will attempt to change
 
the password whose username matches the current user.
 
  
; <code>--database</code><code> DATABASE</code>
+
指定要为用户查询的数据库。 默认为 <code>default</code>
:
 
 
 
Specifies the database to query for the user. Defaults to <code>default</code>.
 
  
 
用法示例:
 
用法示例:
第2,084行: 第1,367行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin changepassword ringo</pre>
+
<syntaxhighlight lang="python">django-admin changepassword ringo</syntaxhighlight>
  
 
</div>
 
</div>
第2,093行: 第1,376行:
 
<div id="createsuperuser" class="section">
 
<div id="createsuperuser" class="section">
  
==== <code>createsuperuser</code> ====
+
==== createsuperuser ====
 
 
; <code>django-admin createsuperuser</code>
 
:
 
 
 
This command is only available if Django's [[../../topics/auth/index|<span class="doc">authentication system</span>]] (<code>django.contrib.auth</code>) is installed.
 
 
 
Creates a superuser account (a user who has all permissions). This is
 
useful if you need to create an initial superuser account or if you need to
 
programmatically generate superuser accounts for your site(s).
 
 
 
When run interactively, this command will prompt for a password for
 
the new superuser account. When run non-interactively, no password
 
will be set, and the superuser account will not be able to log in until
 
a password has been manually set for it.
 
 
 
; <span id="cmdoption-createsuperuser-no-input"></span><code>--noinput</code><code></code><code>, </code><code>--no-input</code><code></code>
 
:
 
  
Suppresses all user prompts. If a suppressed prompt cannot be resolved
+
此命令仅在安装了 Django 的 [[../../topics/auth/index|身份验证系统]] (<code>django.contrib.auth</code>) 时可用。
automatically, the command will exit with error code 1.
 
  
; <code>--username</code><code> USERNAME</code>
+
创建超级用户帐户(拥有所有权限的用户)。 如果您需要创建初始超级用户帐户或需要以编程方式为您的站点生成超级用户帐户,这将非常有用。
:
 
  
; <code>--email</code><code> EMAIL</code>
+
以交互方式运行时,此命令将提示输入新超级用户帐户的密码。 以非交互方式运行时,不会设置密码,并且超级用户帐户将无法登录,直到手动为其设置密码。
:
 
  
The username and email address for the new account can be supplied by
+
禁止所有用户提示。 如果无法自动解决被抑制的提示,该命令将以错误代码 1 退出。
using the <code>--username</code> and <code>--email</code> arguments on the command
 
line. If either of those is not supplied, <code>createsuperuser</code> will prompt for
 
it when running interactively.
 
  
; <code>--database</code><code> DATABASE</code>
+
可以使用命令行上的 <code>--username</code> <code>--email</code> 参数提供新帐户的用户名和电子邮件地址。 如果未提供其中任何一个,<code>createsuperuser</code> 将在交互运行时提示输入。
:
 
  
Specifies the database into which the superuser object will be saved.
+
指定保存超级用户对象的数据库。
  
You can subclass the management command and override <code>get_input_data()</code> if you
+
如果您想自定义数据输入和验证,您可以子类化管理命令并覆盖 <code>get_input_data()</code>。 有关现有实现和方法参数的详细信息,请参阅源代码。 例如,如果您在 [[../../topics/auth/customizing#django.contrib.auth.models.CustomUser|REQUIRED_FIELDS]] 中有一个 <code>ForeignKey</code> 并且希望允许创建实例而不是输入现有实例的主键,则它可能很有用。
want to customize data input and validation. Consult the source code for
 
details on the existing implementation and the method's parameters. For example,
 
it could be useful if you have a <code>ForeignKey</code> in
 
[[../../topics/auth/customizing#django.contrib.auth.models.CustomUser|<code>REQUIRED_FIELDS</code>]] and want to
 
allow creating an instance instead of entering the primary key of an existing
 
instance.
 
  
  
第2,145行: 第1,398行:
 
<div id="django-contrib-contenttypes" class="section">
 
<div id="django-contrib-contenttypes" class="section">
  
=== <code>django.contrib.contenttypes</code> ===
+
=== django.contrib.contenttypes ===
  
 
<div id="remove-stale-contenttypes" class="section">
 
<div id="remove-stale-contenttypes" class="section">
  
==== <code>remove_stale_contenttypes</code> ====
+
==== remove_stale_contenttypes ====
  
; <code>django-admin remove_stale_contenttypes</code>
+
此命令仅在安装了 Django 的 [[../contrib/contenttypes|contenttypes app]] ([[../contrib/contenttypes#module-django.contrib|django.contrib.contenttypes]]) 时可用。
:
 
  
This command is only available if Django's [[../contrib/contenttypes|<span class="doc">contenttypes app</span>]] ([[../contrib/contenttypes#module-django.contrib|<code>django.contrib.contenttypes</code>]]) is installed.
+
删除数据库中过时的内容类型(从已删除的模型中)。 任何依赖于已删除内容类型的对象也将被删除。 在您确认可以继续删除之前,将显示已删除对象的列表。
  
Deletes stale content types (from deleted models) in your database. Any objects
+
指定要使用的数据库。 默认为 <code>default</code>
that depend on the deleted content types will also be deleted. A list of
 
deleted objects will be displayed before you confirm it's okay to proceed with
 
the deletion.
 
 
 
; <span id="cmdoption-remove-stale-contenttypes-database"></span><code>--database</code><code> DATABASE</code>
 
:
 
 
 
Specifies the database to use. Defaults to <code>default</code>.
 
  
  
第2,172行: 第1,416行:
 
<div id="django-contrib-gis" class="section">
 
<div id="django-contrib-gis" class="section">
  
=== <code>django.contrib.gis</code> ===
+
=== django.contrib.gis ===
  
 
<div id="ogrinspect" class="section">
 
<div id="ogrinspect" class="section">
  
==== <code>ogrinspect</code> ====
+
==== ogrinspect ====
  
This command is only available if [[../contrib/gis/index|<span class="doc">GeoDjango</span>]]
+
此命令仅在安装了 [[../contrib/gis/index|GeoDjango]] (<code>django.contrib.gis</code>) 时可用。
(<code>django.contrib.gis</code>) is installed.
 
  
Please refer to its [[../contrib/gis/commands#django-admin-ogrinspect|<code>description</code>]] in the GeoDjango
+
请参考其 [[#id87|:djadmin:`说明 `]] GeoDjango 文档中。
documentation.
 
  
  
第2,190行: 第1,432行:
 
<div id="django-contrib-sessions" class="section">
 
<div id="django-contrib-sessions" class="section">
  
=== <code>django.contrib.sessions</code> ===
+
=== django.contrib.sessions ===
  
 
<div id="clearsessions" class="section">
 
<div id="clearsessions" class="section">
  
==== <code>clearsessions</code> ====
+
==== clearsessions ====
  
; <code>django-admin clearsessions</code>
+
可以以定时任务的形式运行,也可以直接清理过期会话。
:
 
 
 
Can be run as a cron job or directly to clean out expired sessions.
 
  
  
第2,207行: 第1,446行:
 
<div id="django-contrib-sitemaps" class="section">
 
<div id="django-contrib-sitemaps" class="section">
  
=== <code>django.contrib.sitemaps</code> ===
+
=== django.contrib.sitemaps ===
  
 
<div id="ping-google" class="section">
 
<div id="ping-google" class="section">
  
==== <code>ping_google</code> ====
+
==== ping_google ====
  
This command is only available if the [[../contrib/sitemaps|<span class="doc">Sitemaps framework</span>]] (<code>django.contrib.sitemaps</code>) is installed.
+
此命令仅在安装了 [[../contrib/sitemaps|站点地图框架]] (<code>django.contrib.sitemaps</code>) 时可用。
  
Please refer to its [[../contrib/sitemaps#django-admin-ping_google|<code>description</code>]] in the Sitemaps
+
请参考其 [[#id89|:djadmin:`说明 `]] 在站点地图文档中。
documentation.
 
  
  
第2,224行: 第1,462行:
 
<div id="django-contrib-staticfiles" class="section">
 
<div id="django-contrib-staticfiles" class="section">
  
=== <code>django.contrib.staticfiles</code> ===
+
=== django.contrib.staticfiles ===
  
 
<div id="collectstatic" class="section">
 
<div id="collectstatic" class="section">
  
==== <code>collectstatic</code> ====
+
==== collectstatic ====
  
This command is only available if the [[../../howto/static-files/index|<span class="doc">static files application</span>]] (<code>django.contrib.staticfiles</code>) is installed.
+
此命令仅在安装了 [[../../howto/static-files/index|静态文件应用程序]] (<code>django.contrib.staticfiles</code>) 时可用。
  
Please refer to its [[../contrib/staticfiles#django-admin-collectstatic|<code>description</code>]] in the
+
请参考其 [[#id91|:djadmin:`说明 `]] 在里面[[../contrib/staticfiles|静态文件]]文档。
[[../contrib/staticfiles|<span class="doc">staticfiles</span>]] documentation.
 
  
  
第2,239行: 第1,476行:
 
<div id="findstatic" class="section">
 
<div id="findstatic" class="section">
  
==== <code>findstatic</code> ====
+
==== findstatic ====
  
This command is only available if the [[../../howto/static-files/index|<span class="doc">static files application</span>]] (<code>django.contrib.staticfiles</code>) is installed.
+
此命令仅在安装了 [[../../howto/static-files/index|静态文件应用程序]] (<code>django.contrib.staticfiles</code>) 时可用。
  
Please refer to its [[../contrib/staticfiles#django-admin-findstatic|<code>description</code>]] in the [[../contrib/staticfiles|<span class="doc">staticfiles</span>]] documentation.
+
请参考其 [[#id93|:djadmin:`说明 `]] 在里面[[../contrib/staticfiles|静态文件]]文档。
  
  
第2,253行: 第1,490行:
 
<div id="default-options" class="section">
 
<div id="default-options" class="section">
  
== Default options ==
+
== 默认选项 ==
  
Although some commands may allow their own custom options, every command
+
虽然有些命令可能允许自己的自定义选项,但每个命令都允许以下选项:
allows for the following options:
 
  
; <code>--pythonpath</code><code> PYTHONPATH</code>
+
将给定的文件系统路径添加到 Python [https://www.diveinto.org/python3/your-first-python-program.html#importsearchpath 导入搜索路径] 。 如果未提供,<code>django-admin</code> 将使用 <code>PYTHONPATH</code> 环境变量。
:
 
  
Adds the given filesystem path to the Python [https://www.diveinto.org/python3/your-first-python-program.html#importsearchpath import search path]. If this
+
这个选项在 <code>manage.py</code> 中是不必要的,因为它负责为你设置 Python 路径。
isn't provided, <code>django-admin</code> will use the <code>PYTHONPATH</code> environment
 
variable.
 
 
 
This option is unnecessary in <code>manage.py</code>, because it takes care of setting
 
the Python path for you.
 
  
 
用法示例:
 
用法示例:
第2,274行: 第1,504行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin migrate --pythonpath='/home/djangoprojects/myproject'</pre>
+
<syntaxhighlight lang="python">django-admin migrate --pythonpath='/home/djangoprojects/myproject'</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--settings</code><code> SETTINGS</code>
+
指定要使用的设置模块。 设置模块应该采用 Python 包语法,例如 <code>mysite.settings</code>。 如果未提供,<code>django-admin</code> 将使用 <code>DJANGO_SETTINGS_MODULE</code> 环境变量。
:
 
  
Specifies the settings module to use. The settings module should be in Python
+
<code>manage.py</code> 中不需要此选项,因为它默认使用当前项目中的 <code>settings.py</code>
package syntax, e.g. <code>mysite.settings</code>. If this isn't provided,
 
<code>django-admin</code> will use the <code>DJANGO_SETTINGS_MODULE</code> environment variable.
 
 
 
This option is unnecessary in <code>manage.py</code>, because it uses
 
<code>settings.py</code> from the current project by default.
 
  
 
用法示例:
 
用法示例:
第2,295行: 第1,519行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin migrate --settings=mysite.settings</pre>
+
<syntaxhighlight lang="python">django-admin migrate --settings=mysite.settings</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--traceback</code><code></code>
+
引发 [[../../howto/custom-management-commands#django.core.management|CommandError]] 时显示完整的堆栈跟踪。 默认情况下,当 <code>CommandError</code> 发生时,<code>django-admin</code> 将显示一条简单的错误消息,以及任何其他异常的完整堆栈跟踪。
:
 
 
 
Displays a full stack trace when a [[../../howto/custom-management-commands#django.core.management|<code>CommandError</code>]]
 
is raised. By default, <code>django-admin</code> will show a simple error message when a
 
<code>CommandError</code> occurs and a full stack trace for any other exception.
 
  
 
用法示例:
 
用法示例:
第2,313行: 第1,532行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin migrate --traceback</pre>
+
<syntaxhighlight lang="python">django-admin migrate --traceback</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <span id="cmdoption-v"></span><code>--verbosity</code><code> {0,1,2,3}</code><code>, </code><code>-v</code><code> {0,1,2,3}</code>
+
指定命令应打印到控制台的通知和调试信息的数量。
:
 
  
Specifies the amount of notification and debug information that a command
+
* <code>0</code> 表示无输出。
should print to the console.
+
* <code>1</code> 表示正常输出(默认)。
 
+
* <code>2</code> 表示详细输出。
* <code>0</code> means no output.
+
* <code>3</code> 表示 ''very'' 详细输出。
* <code>1</code> means normal output (default).
 
* <code>2</code> means verbose output.
 
* <code>3</code> means ''very'' verbose output.
 
  
 
用法示例:
 
用法示例:
第2,335行: 第1,550行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin migrate --verbosity 2</pre>
+
<syntaxhighlight lang="python">django-admin migrate --verbosity 2</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--no-color</code><code></code>
+
禁用彩色命令输出。 一些命令将其输出格式化为彩色。 例如,错误将以红色打印到控制台,SQL 语句将以语法高亮显示。
:
 
 
 
Disables colorized command output. Some commands format their output to be
 
colorized. For example, errors will be printed to the console in red and SQL
 
statements will be syntax highlighted.
 
  
 
用法示例:
 
用法示例:
第2,353行: 第1,563行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>django-admin runserver --no-color</pre>
+
<syntaxhighlight lang="python">django-admin runserver --no-color</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
; <code>--force-color</code><code></code>
 
:
 
 
 
<div class="versionadded">
 
<div class="versionadded">
  
 +
<span class="versionmodified added">2.2 版中的新功能。</span>
  
  
 
</div>
 
</div>
Forces colorization of the command output if it would otherwise be disabled
+
如果命令输出被禁用,如 [[#syntax-coloring|语法着色]] 中所述,则强制对其进行着色。 例如,您可能希望将彩色输出通过管道传送到另一个命令。
as discussed in [[#syntax-coloring|<span class="std std-ref">Syntax coloring</span>]]. For example, you may want to pipe
 
colored output to another command.
 
  
  
第2,374行: 第1,580行:
 
<div id="extra-niceties" class="section">
 
<div id="extra-niceties" class="section">
  
== Extra niceties ==
+
== 额外的细节 ==
  
 
<div id="syntax-coloring" class="section">
 
<div id="syntax-coloring" class="section">
  
<span id="id1"></span>
+
<span id="id95"></span>
=== Syntax coloring ===
+
=== 语法着色 ===
  
The <code>django-admin</code> / <code>manage.py</code> commands will use pretty
+
如果您的终端支持 ANSI 彩色输出,<code>django-admin</code> / <code>manage.py</code> 命令将使用漂亮的彩色编码输出。 除非使用 <code>--force-color</code> 选项,否则如果您将命令的输出通过管道传输到另一个程序,它将不会使用颜色代码。
color-coded output if your terminal supports ANSI-colored output. It
 
won't use the color codes if you're piping the command's output to
 
another program unless the [[#cmdoption-force-color|<code>--force-color</code>]] option is used.
 
  
Under Windows, the native console doesn't support ANSI escape sequences so by
+
Windows 下,本机控制台不支持 ANSI 转义序列,因此默认情况下没有颜色输出。 但是您可以安装 [http://adoxa.altervista.org/ansicon/ ANSICON] 第三方工具,Django 命令将检测它的存在并利用其服务对输出进行着色,就像在基于 Unix 的平台上一样。
default there is no color output. But you can install the [http://adoxa.altervista.org/ansicon/ ANSICON]
 
third-party tool, the Django commands will detect its presence and will make
 
use of its services to color output just like on Unix-based platforms.
 
  
The colors used for syntax highlighting can be customized. Django
+
可以自定义用于语法突出显示的颜色。 Django 附带三个调色板:
ships with three color palettes:
 
  
* <code>dark</code>, suited to terminals that show white text on a black background. This is the default palette.
+
* <code>dark</code>,适用于黑底白字的终端。 这是默认调色板。
* <code>light</code>, suited to terminals that show black text on a white background.
+
* <code>light</code>,适用于白底黑字的终端。
* <code>nocolor</code>, which disables syntax highlighting.
+
* <code>nocolor</code>,禁用语法高亮。
  
You select a palette by setting a <code>DJANGO_COLORS</code> environment
+
您可以通过设置 <code>DJANGO_COLORS</code> 环境变量来指定要使用的调色板来选择调色板。 例如,要在 Unix 或 OS/X BASH shell 下指定 <code>light</code> 调色板,您可以在命令提示符下运行以下命令:
variable to specify the palette you want to use. For example, to
 
specify the <code>light</code> palette under a Unix or OS/X BASH shell, you
 
would run the following at a command prompt:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,407行: 第1,603行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>export DJANGO_COLORS=&quot;light&quot;</pre>
+
<syntaxhighlight lang="python">export DJANGO_COLORS="light"</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
You can also customize the colors that are used. Django specifies a
+
您还可以自定义使用的颜色。 Django 指定了许多使用颜色的角色:
number of roles in which color is used:
 
  
* <code>error</code> - A major error.
+
* <code>error</code> - 重大错误。
* <code>notice</code> - A minor error.
+
* <code>notice</code> - 一个小错误。
* <code>success</code> - A success.
+
* <code>success</code> - 成功。
* <code>warning</code> - A warning.
+
* <code>warning</code> - 警告。
* <code>sql_field</code> - The name of a model field in SQL.
+
* <code>sql_field</code> - SQL 中模型字段的名称。
* <code>sql_coltype</code> - The type of a model field in SQL.
+
* <code>sql_coltype</code> - SQL 中模型字段的类型。
* <code>sql_keyword</code> - An SQL keyword.
+
* <code>sql_keyword</code> - SQL 关键字。
* <code>sql_table</code> - The name of a model in SQL.
+
* <code>sql_table</code> - SQL 中模型的名称。
* <code>http_info</code> - A 1XX HTTP Informational server response.
+
* <code>http_info</code> - 1XX HTTP 信息服务器响应。
* <code>http_success</code> - A 2XX HTTP Success server response.
+
* <code>http_success</code> - 2XX HTTP 成功服务器响应。
* <code>http_not_modified</code> - A 304 HTTP Not Modified server response.
+
* <code>http_not_modified</code> - 304 HTTP 未修改服务器响应。
* <code>http_redirect</code> - A 3XX HTTP Redirect server response other than 304.
+
* <code>http_redirect</code> - 304 以外的 3XX HTTP 重定向服务器响应。
* <code>http_not_found</code> - A 404 HTTP Not Found server response.
+
* <code>http_not_found</code> - 404 HTTP Not Found 服务器响应。
* <code>http_bad_request</code> - A 4XX HTTP Bad Request server response other than 404.
+
* <code>http_bad_request</code> - 404 以外的 4XX HTTP 错误请求服务器响应。
* <code>http_server_error</code> - A 5XX HTTP Server Error response.
+
* <code>http_server_error</code> - 5XX HTTP 服务器错误响应。
* <code>migrate_heading</code> - A heading in a migrations management command.
+
* <code>migrate_heading</code> - 迁移管理命令中的标题。
* <code>migrate_label</code> - A migration name.
+
* <code>migrate_label</code> - 迁移名称。
  
Each of these roles can be assigned a specific foreground and
+
这些角色中的每一个都可以从以下列表中指定特定的前景和背景颜色:
background color, from the following list:
 
  
 
* <code>black</code>
 
* <code>black</code>
第2,445行: 第1,639行:
 
* <code>white</code>
 
* <code>white</code>
  
Each of these colors can then be modified by using the following
+
然后可以通过使用以下显示选项来修改这些颜色:
display options:
 
  
 
* <code>bold</code>
 
* <code>bold</code>
第2,454行: 第1,647行:
 
* <code>conceal</code>
 
* <code>conceal</code>
  
A color specification follows one of the following patterns:
+
颜色规格遵循以下模式之一:
  
 
* <code>role=fg</code>
 
* <code>role=fg</code>
第2,461行: 第1,654行:
 
* <code>role=fg/bg,option,option</code>
 
* <code>role=fg/bg,option,option</code>
  
where <code>role</code> is the name of a valid color role, <code>fg</code> is the
+
其中 <code>role</code> 是有效颜色角色的名称,<code>fg</code> 是前景色,<code>bg</code> 是背景色,每个 <code>option</code> 是其中一种颜色修改选项。 然后用分号分隔多个颜色规格。 例如:
foreground color, <code>bg</code> is the background color and each <code>option</code>
 
is one of the color modifying options. Multiple color specifications
 
are then separated by a semicolon. For example:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,470行: 第1,660行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>export DJANGO_COLORS=&quot;error=yellow/blue,blink;notice=magenta&quot;</pre>
+
<syntaxhighlight lang="python">export DJANGO_COLORS="error=yellow/blue,blink;notice=magenta"</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would specify that errors be displayed using blinking yellow on blue,
+
将指定使用蓝色闪烁黄色显示错误,并使用洋红色显示通知。 所有其他颜色角色将保持不变。
and notices displayed using magenta. All other color roles would be
 
left uncolored.
 
  
Colors can also be specified by extending a base palette. If you put
+
还可以通过扩展基本调色板来指定颜色。 如果您将调色板名称放在颜色规范中,则该调色板隐含的所有颜色都将被加载。 所以:
a palette name in a color specification, all the colors implied by that
 
palette will be loaded. So:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,487行: 第1,673行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>export DJANGO_COLORS=&quot;light;error=yellow/blue,blink;notice=magenta&quot;</pre>
+
<syntaxhighlight lang="python">export DJANGO_COLORS="light;error=yellow/blue,blink;notice=magenta"</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
would specify the use of all the colors in the light color palette,
+
将指定使用浅色调色板中的所有颜色, ''除外'' 用于错误和通知的颜色,这些颜色将按指定被覆盖。
''except'' for the colors for errors and notices which would be
 
overridden as specified.
 
  
  
第2,500行: 第1,684行:
 
<div id="bash-completion" class="section">
 
<div id="bash-completion" class="section">
  
=== Bash completion ===
+
=== Bash 补全 ===
  
If you use the Bash shell, consider installing the Django bash completion
+
如果您使用 Bash shell,请考虑安装 Django bash 完成脚本,该脚本位于 Django 源代码分发中的 <code>extras/django_bash_completion</code> 中。 它启用 <code>django-admin</code> <code>manage.py</code> 命令的制表符补全,因此您可以,例如……
script, which lives in <code>extras/django_bash_completion</code> in the Django source
 
distribution. It enables tab-completion of <code>django-admin</code> and
 
<code>manage.py</code> commands, so you can, for instance...
 
  
* Type <code>django-admin</code>.
+
* 输入 <code>django-admin</code>
* Press [TAB] to see all available options.
+
* [TAB] 查看所有可用选项。
* Type <code>sql</code>, then [TAB], to see all available options whose names start with <code>sql</code>.
+
* 输入 <code>sql</code>,然后输入 [TAB],以查看名称以 <code>sql</code> 开头的所有可用选项。
  
See [[../../howto/custom-management-commands|<span class="doc">编写自定义 django-admin 命令</span>]] for how to add customized actions.
+
有关如何添加自定义操作,请参阅 [[../../howto/custom-management-commands|编写自定义 django-admin 命令]]
  
  
第2,521行: 第1,702行:
 
<div id="running-management-commands-from-your-code" class="section">
 
<div id="running-management-commands-from-your-code" class="section">
  
= Running management commands from your code =
+
= 从你的代码中运行管理命令 =
  
; <code>django.core.management.</code><code>call_command</code><span class="sig-paren">(</span>''<span class="n">name</span>'', ''<span class="o">*</span><span class="n">args</span>'', ''<span class="o">**</span><span class="n">options</span>''<span class="sig-paren">)</span>
+
; <span class="sig-prename descclassname"><span class="pre">django.core.management.</span></span><span class="sig-name descname"><span class="pre">call_command</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">name</span></span>'', ''<span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span>'', ''<span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">options</span></span>''<span class="sig-paren">)</span>
 
:  
 
:  
  
To call a management command from code use <code>call_command</code>.
+
要从代码中调用管理命令,请使用 <code>call_command</code>
  
 
; <code>name</code>
 
; <code>name</code>
: the name of the command to call or a command object. Passing the name is preferred unless the object is required for testing.
+
: 要调用的命令的名称或命令对象。 除非测试需要对象,否则首选传递名称。
 
; <code>*args</code>
 
; <code>*args</code>
: a list of arguments accepted by the command. Arguments are passed to the argument parser, so you can use the same style as you would on the command line. For example, <code>call_command('flush', '--verbosity=0')</code>.
+
: 命令接受的参数列表。 参数传递给参数解析器,因此您可以使用与在命令行上相同的样式。 例如,<code>call_command('flush', '--verbosity=0')</code>
 
; <code>**options</code>
 
; <code>**options</code>
: named options accepted on the command-line. Options are passed to the command without triggering the argument parser, which means you'll need to pass the correct type. For example, <code>call_command('flush', verbosity=0)</code> (zero must be an integer rather than a string).
+
: 命令行上接受的命名选项。 选项被传递给命令而不触发参数解析器,这意味着您需要传递正确的类型。 例如,<code>call_command('flush', verbosity=0)</code>(零必须是整数而不是字符串)。
  
举例:
+
实际案例
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,541行: 第1,722行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.core import management
+
<syntaxhighlight lang="python">from django.core import management
 
from django.core.management.commands import loaddata
 
from django.core.management.commands import loaddata
  
 
management.call_command('flush', verbosity=0, interactive=False)
 
management.call_command('flush', verbosity=0, interactive=False)
 
management.call_command('loaddata', 'test_data', verbosity=0)
 
management.call_command('loaddata', 'test_data', verbosity=0)
management.call_command(loaddata.Command(), 'test_data', verbosity=0)</pre>
+
management.call_command(loaddata.Command(), 'test_data', verbosity=0)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Note that command options that take no arguments are passed as keywords
+
请注意,不带参数的命令选项作为关键字与 <code>True</code> <code>False</code> 一起传递,如您在上面的 <code>interactive</code> 选项中所见。
with <code>True</code> or <code>False</code>, as you can see with the <code>interactive</code> option above.
 
  
Named arguments can be passed by using either one of the following syntaxes:
+
可以使用以下任一语法传递命名参数:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,560行: 第1,740行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre># Similar to the command line
+
<syntaxhighlight lang="python"># Similar to the command line
 
management.call_command('dumpdata', '--natural-foreign')
 
management.call_command('dumpdata', '--natural-foreign')
  
第2,568行: 第1,748行:
  
 
# `use_natural_foreign_keys` is the option destination variable
 
# `use_natural_foreign_keys` is the option destination variable
management.call_command('dumpdata', use_natural_foreign_keys=True)</pre>
+
management.call_command('dumpdata', use_natural_foreign_keys=True)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
Some command options have different names when using <code>call_command()</code> instead
+
使用 <code>call_command()</code> 而不是 <code>django-admin</code> <code>manage.py</code> 时,某些命令选项具有不同的名称。 例如,<code>django-admin createsuperuser --no-input</code> 转换为 <code>call_command('createsuperuser', interactive=False)</code>。 要查找用于 <code>call_command()</code> 的关键字参数名称,请检查传递给 <code>parser.add_argument()</code> <code>dest</code> 参数的命令源代码。
of <code>django-admin</code> or <code>manage.py</code>. For example, <code>django-admin createsuperuser --no-input</code> translates to <code>call_command('createsuperuser', interactive=False)</code>. To find what keyword argument name to use for
 
<code>call_command()</code>, check the command's source code for the <code>dest</code> argument
 
passed to <code>parser.add_argument()</code>.
 
  
Command options which take multiple options are passed a list:
+
接受多个选项的命令选项会传递一个列表:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,584行: 第1,761行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>management.call_command('dumpdata', exclude=['contenttypes', 'auth'])</pre>
+
<syntaxhighlight lang="python">management.call_command('dumpdata', exclude=['contenttypes', 'auth'])</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The return value of the <code>call_command()</code> function is the same as the return
+
<code>call_command()</code>函数的返回值与命令的<code>handle()</code>方法的返回值相同。
value of the <code>handle()</code> method of the command.
 
  
 
<div id="output-redirection" class="section">
 
<div id="output-redirection" class="section">
  
== Output redirection ==
+
== 输出重定向 ==
  
Note that you can redirect standard output and error streams as all commands
+
请注意,您可以重定向标准输出和错误流,因为所有命令都支持 <code>stdout</code> <code>stderr</code> 选项。 例如,你可以写:
support the <code>stdout</code> and <code>stderr</code> options. For example, you could write:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第2,603行: 第1,778行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>with open('/path/to/command_output', 'w') as f:
+
<syntaxhighlight lang="python">with open('/path/to/command_output', 'w') as f:
     management.call_command('dumpdata', stdout=f)</pre>
+
     management.call_command('dumpdata', stdout=f)</syntaxhighlight>
 +
 
 +
</div>
  
 
</div>
 
</div>
第2,611行: 第1,788行:
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Django 2.2.x 中文文档]]
+
[[Category:Django 2.2.x 文档]]

2021年10月31日 (日) 04:05的最新版本

django-admin 和 manage.py

django-admin 是 Django 用于管理任务的命令行实用程序。 本文档概述了它可以做的所有事情。

另外,manage.py 是在每个 Django 项目中自动创建的。 它与 django-admin 执行相同的操作,但还设置了 DJANGO_SETTINGS_MODULE 环境变量,使其指向您项目的 settings.py 文件。

如果您通过 pip 安装了 Django,那么 django-admin 脚本应该在您的系统路径上。 如果它不在您的路径中,您可以在 Python 安装的 site-packages/django/bin 中找到它。 考虑从路径上的某个位置对其进行符号链接,例如 /usr/local/bin

对于没有可用符号链接功能的 Windows 用户,您可以将 django-admin.exe 复制到现有路径上的某个位置或编辑 PATH 设置(在 Settings - Control Panel - System - Advanced - Environment... 下)以指向它的安装位置。

通常,在处理单个 Django 项目时,使用 manage.py 比使用 django-admin 更容易。 如果您需要在多个 Django 设置文件之间切换,请使用 django-adminDJANGO_SETTINGS_MODULE--settings 命令行选项。

本文档中的命令行示例使用 django-admin 以保持一致,但任何示例也可以使用 manage.pypython -m django

如何使用

command 应该是本文档中列出的命令之一。 options 是可选的,应该是零个或多个可用于给定命令的选项。

获得运行时帮助

运行 django-admin help 以显示使用信息和每个应用程序提供的命令列表。

运行 django-admin help --commands 以显示所有可用命令的列表。

运行 django-admin help <command> 以显示给定命令的描述及其可用选项列表。


应用名称

许多命令采用“应用程序名称”列表。 “应用程序名称”是包含模型的包的基本名称。 例如,如果您的 :setting:`INSTALLED_APPS` 包含字符串 'mysite.blog',则应用名称为 blog


确定版本

运行 django-admin version 以显示当前的 Django 版本。

输出遵循 PEP 440 中描述的架构:

1.4.dev17026
1.4a1
1.4

显示 debug 输出

使用 --verbosity 指定 django-admin 打印到控制台的通知和调试信息的数量。


可用命令

check

使用系统检查框架检查整个Django项目的常见问题。

默认情况下,将检查所有应用程序。 您可以通过提供应用标签列表作为参数来检查应用的子集:

django-admin check auth admin myapp

如果您不指定任何应用程序,则将检查所有应用程序。

系统检查框架执行许多不同类型的检查,这些检查是 用标签 分类的。 您可以使用这些标签将执行的检查限制为仅针对特定类别中的检查。 例如,要仅执行模型和兼容性检查,请运行:

django-admin check --tag models --tag compatibility

列出所有可用的标签。

激活一些仅在部署环境中相关的附加检查。

您可以在本地开发环境中使用此选项,但由于您的本地开发设置模块可能没有许多生产设置,您可能希望将 check 命令指向不同的设置模块,通过设置DJANGO_SETTINGS_MODULE 环境变量,或通过传递 --settings 选项:

django-admin check --deploy --settings=production_settings

或者您可以直接在生产或临时部署上运行它以验证是否使用了正确的设置(省略 --settings)。 您甚至可以将其作为集成测试套件的一部分。

指定将导致命令以非零状态退出的消息级别。 默认值为 ERROR


compilemessages

:djadmin:`makemessages` 创建的 .po 文件编译为 .mo 文件,以便与内置的 gettext 支持一起使用。 参见 国际化和本地化

指定要处理的语言环境。 如果未提供,则处理所有语言环境。

指定要从处理中排除的区域设置。 如果未提供,则不排除任何区域设置。

包括 `fuzzy 翻译`_ 到编译文件。

用法示例:

django-admin compilemessages --locale=pt_BR
django-admin compilemessages --locale=pt_BR --locale=fr -f
django-admin compilemessages -l pt_BR
django-admin compilemessages -l pt_BR -l fr --use-fuzzy
django-admin compilemessages --exclude=pt_BR
django-admin compilemessages --exclude=pt_BR --exclude=fr
django-admin compilemessages -x pt_BR
django-admin compilemessages -x pt_BR -x fr

createcachetable

使用设置文件中的信息创建用于数据库缓存后端的缓存表。 有关更多信息,请参阅 Django 的缓存框架

指定将在其中创建缓存表的数据库。 默认为 default

打印无需实际运行的 SQL,所以你可以自定义它或使用迁移框架。


dbshell

为您的数据库引擎中指定的数据库引擎运行命令行客户端 :设置:`引擎 ` 设置,使用您在 :设置:`用户` , :设置:`密码` 等,设置。

  • 对于 PostgreSQL,它运行 psql 命令行客户端。
  • 对于 MySQL,这将运行 mysql 命令行客户端。
  • 对于 SQLite,它运行 sqlite3 命令行客户端。
  • 对于 Oracle,这将运行 sqlplus 命令行客户端。

此命令假定程序在您的 PATH 上,因此对程序名称的简单调用 (psqlmysqlsqlite3sqlplus ]) 将在正确的位置找到该程序。 无法手动指定程序的位置。

指定要在其上打开 shell 的数据库。 默认为 default

笔记

请注意,并非所有在 :setting:`DATABASES` 中的数据库配置的 :setting:`OPTIONS` 部分中设置它的选项都会传递给命令行客户端,例如 'isolation_level'


diffsettings

显示当前设置文件和 Django 的默认设置(或由 --default 指定的另一个设置文件)之间的差异。

默认值中没有出现的设置后面跟着"###"。 例如,默认设置没有定义 :setting:`ROOT_URLCONF`,所以 :setting:`ROOT_URLCONF`"###" X159X]。

显示所有设置,即使它们具有 Django 的默认值。 此类设置以 "###" 为前缀。

用于比较当前设置的设置模块。 留空以与 Django 的默认设置进行比较。

指定输出格式。 可用值为 hashunifiedhash 是显示上述输出的默认模式。 unified 显示类似于 diff -u 的输出。 默认设置以减号为前缀,后跟以加号为前缀的更改设置。


dumpdata

将数据库中与指定应用程序相关联的所有数据输出到标准输出。

如果没有提供应用程序名称,所有安装的应用程序将被转储。

dumpdata 的输出可以用作 :djadmin:`loaddata` 的输入。

请注意,dumpdata 使用模型上的默认管理器来选择要转储的记录。 如果您使用 自定义管理器 作为默认管理器并且它过滤了一些可用记录,则并非所有对象都将被转储。

使用 Django 的基本管理器,转储可能会被自定义管理器过滤或修改的记录。

指定输出的序列化格式。 默认为 JSON。 支持的格式在序列化格式中列出。

指定要在输出中使用的缩进空格数。 默认为 None,在一行上显示所有数据。

防止转储特定的应用程序或模型(以 app_label.ModelName 的形式指定)。 如果您指定模型名称,则输出将仅限于该模型,而不是整个应用程序。 您还可以混合应用程序名称和型号名称。

如果要排除多个应用程序,请多次传递 --exclude

django-admin dumpdata --exclude=auth --exclude=contenttypes

指定将转储数据的数据库。 默认为 default

使用 natural_key() 模型方法将任何外键和多对多关系序列化到定义该方法的类型的对象。 如果您要转储 contrib.auth Permission 对象或 contrib.contenttypes ContentType 对象,您可能应该使用此标志。 有关此选项和下一个选项的更多详细信息,请参阅 自然键 文档。

省略该对象序列化数据中的主键,因为它可以在反序列化过程中计算。

仅输出由逗号分隔的主键列表指定的对象。 这仅在倾销一种模型时可用。 默认输出模型的所有记录。

指定要写入序列化数据的文件。 默认情况下,数据进入标准输出。

当设置此选项且 --verbosity 大于 0(默认)时,终端中会显示进度条。


flush

从数据库中删除所有数据并重新执行任何同步后处理程序。 已应用迁移的表不会被清除。

如果您宁愿从空数据库开始并重新运行所有迁移,您应该删除并重新创建数据库,然后运行 :djadmin:`migrate`

禁止所有的用户提示。

指定要刷新的数据库。 默认为 default


inspectdb

内省 :setting:`NAME` 设置指向的数据库中的数据库表,并将 Django 模型模块(models.py 文件)输出到标准输出。

您可以通过将名称作为参数传递来选择要检查的表或视图。 如果未提供参数,则仅在使用 --include-views 选项时才为视图创建模型。 如果使用 --include-partitions 选项,则在 PostgreSQL 上创建分区表模型。

如果您有一个要使用 Django 的旧数据库,请使用此选项。 该脚本将检查数据库并为其中的每个表创建一个模型。

正如您所料,创建的模型将具有表中每个字段的属性。 请注意, inspectdb 在其字段名输出中有一些特殊情况:

  • 如果 inspectdb 无法将列的类型映射到模型字段类型,它将使用 TextField 并将在生成的模型中的字段旁边插入 Python 注释 'This field type is a guess.'。 识别的字段可能取决于 :setting:`INSTALLED_APPS` 中列出的应用程序。 例如,django.contrib.postgres 添加了对几种 PostgreSQL 特定字段类型的识别。
  • 如果数据库列名是 Python 保留字(例如 'pass''class''for'),则 inspectdb 将附加 '_field'属性名称。 例如,如果一个表有一个列 'for',生成的模型将有一个字段 'for_field'db_column 属性设置为 'for'inspectdb 将在字段旁边插入 Python 注释 'Field renamed because it was a Python reserved word.'

此功能是一种快捷方式,而不是确定的模型生成。 运行后,您需要自己查看生成的模型以进行自定义。 特别是,您需要重新排列模型的顺序,以便正确排列引用其他模型的模型。

当在模型字段上指定 default 时,Django 不会创建数据库默认值。 同样,数据库默认值不会转换为模型字段默认值,也不会被 inspectdb 以任何方式检测到。

默认情况下,inspectdb 创建非托管模型。 也就是说,模型的 Meta 类中的 managed = False 告诉 Django 不要管理每个表的创建、修改和删除。 如果您确实想让 Django 管理表的生命周期,则需要将 managed 选项更改为 True(或者简单地将其删除,因为 True 是其默认值价值)。

特定于数据库的注释

Oracle
  • 如果使用 --include-views,则为实体化视图创建模型。


PostgreSQL
  • 为外部表创建模型。
  • 如果使用 --include-views,则为实体化视图创建模型。
  • 如果使用 --include-partitions,则为分区表创建模型。

2.2 版本变化: 添加了对外部表和物化视图的支持。


指定要内省的数据库。 默认为 default

2.2 版中的新功能。


如果提供了这个选项,也会为分区创建模型。

只实现了对 PostgreSQL 的支持。

2.1 版中的新功能。


如果提供了这个选项,也会为数据库视图创建模型。


loaddata

搜索并将已命名固定数据中的内容加载到数据库中。

指定数据将加载到的数据库。 默认为 default

忽略自固定数据最初生成以来可能已经被删除的字段和模型。

指定一个单一的应用来寻找固定数据,而不是在所有的应用程序中寻找。

为从 stdin 读取的装置 指定 序列化格式 (例如,jsonxml)。

不包括从给定的应用程序和/或模型(以 app_labelapp_label.ModelName 的形式)加载灯具。 多次使用该选项以排除多个应用程序或模型。

什么是“夹具”?

fixture 是包含数据库序列化内容的文件集合。 每个夹具都有一个唯一的名称,构成夹具的文件可以分布在多个目录、多个应用程序中。

Django 会在三个位置搜索固定数据:

  1. 在每个已安装应用程序的 fixtures 目录中
  2. :setting:`FIXTURE_DIRS` 设置中命名的任何目录中
  3. 在由固定数据命名的文字路径中

Django 将加载它在这些位置找到的与所提供的固定数据名称相匹配的任何和所有固定数据。

如果命名装置具有文件扩展名,则只会加载该类型的装置。 例如:

django-admin loaddata mydata.json

只会加载名为 mydata 的 JSON 固定装置。 夹具扩展名必须对应于 串行器 的注册名称(例如,jsonxml)。

如果省略扩展名,Django 将搜索所有可用的设备类型以查找匹配的设备。 例如:

django-admin loaddata mydata

将寻找任何名为 mydata 的夹具类型的任何夹具。 如果夹具目录包含 mydata.json,则该夹具将作为 JSON 夹具加载。

命名的装置可以包含目录组件。 这些目录将包含在搜索路径中。 例如:

django-admin loaddata foo/bar/mydata.json

将为每个已安装的应用程序搜索 <app_label>/fixtures/foo/bar/mydata.json,为 :setting:`FIXTURE_DIRS` 中的每个目录搜索 <dirname>/foo/bar/mydata.json,以及文字路径 foo/bar/mydata.json

处理夹具文件时,数据按原样保存到数据库中。 模型定义的 save() 方法不会被调用,并且任何 pre_savepost_save 信号将使用 raw=True 调用,因为实例只包含属性是模型本地的。 例如,您可能想要禁用访问在夹具加载期间不存在的相关字段的处理程序,否则会引发异常:

from django.db.models.signals import post_save
from .models import MyModel

def my_handler(**kwargs):
    # disable the handler during fixture loading
    if kwargs['raw']:
        return
    ...

post_save.connect(my_handler, sender=MyModel)

您还可以编写一个简单的装饰器来封装此逻辑:

from functools import wraps

def disable_for_loaddata(signal_handler):
    """
    Decorator that turns off signal handlers when loading fixture data.
    """
    @wraps(signal_handler)
    def wrapper(*args, **kwargs):
        if kwargs['raw']:
            return
        signal_handler(*args, **kwargs)
    return wrapper

@disable_for_loaddata
def my_handler(**kwargs):
    ...

请注意,此逻辑将在灯具反序列化时禁用信号,而不仅仅是在 loaddata 期间。

请注意,夹具文件的处理顺序是未定义的。 但是,所有夹具数据都作为单个事务安装,因此一个夹具中的数据可以引用另一个夹具中的数据。 如果数据库后端支持行级约束,则会在事务结束时检查这些约束。

:djadmin:`dumpdata` 命令可用于为 loaddata 生成输入。


压缩的固定数据

灯具可以压缩为 zipgzbz2 格式。 例如:

django-admin loaddata mydata.json

将查找 mydata.jsonmydata.json.zipmydata.json.gzmydata.json.bz2 中的任何一个。 使用 zip 压缩存档中包含的第一个文件。

请注意,如果发现两个同名但灯具类型不同的灯具(例如,如果在同一灯具目录中发现 mydata.jsonmydata.xml.gz),则灯具安装将被中止,并且任何在调用 loaddata 时安装的数据将从数据库中删除。

使用 MyISAM 的 MySQL 与固定数据

MySQL 的 MyISAM 存储引擎不支持事务或约束,因此如果您使用 MyISAM,您将不会得到夹具数据的验证,或者如果找到多个事务文件则回滚。


特定数据库的固定数据

如果您处于多数据库设置中,您可能希望将夹具数据加载到一个数据库中,但不想加载到另一个数据库中。 在这种情况下,您可以将数据库标识符添加到灯具的名称中。

例如,如果您的 :setting:`DATABASES` 设置定义了一个“主”数据库,则将夹具命名为 mydata.master.jsonmydata.master.json.gz,并且夹具将仅在您指定要将数据加载到 master 数据库中。


从 stdin 加载夹具

您可以使用破折号作为灯具名称来加载来自 sys.stdin 的输入。 例如:

django-admin loaddata --format=json -

stdin读取时,需要--format选项来指定输入的序列化格式(例如,jsonxml )。

stdin 加载对于标准输入和输出重定向很有用。 例如:

django-admin dumpdata --format=json --database=test app_label.ModelName | django-admin loaddata --format=json --database=prod -

makemessages

遍历当前目录的整个源代码树并提取所有标记为翻译的字符串。 它在 conf/locale(在 Django 树中)或 locale(用于项目和应用程序)目录中创建(或更新)一个消息文件。 对消息文件进行更改后,您需要使用 :djadmin:`compilemessages` 编译它们,以便与内置的 gettext 支持一起使用。 有关详细信息,请参阅 i18n 文档

此命令不需要配置设置。 但是,当没有配置设置时,该命令不能忽略 :setting:`MEDIA_ROOT`:setting:`STATIC_ROOT` 目录或包含 :setting: `LOCALE_PATHS`

更新所有可用语言的消息文件。

指定要检查的文件扩展名列表(默认值:htmltxtpyjs 如果 --domain 是 [ X122X])。

用法示例:

django-admin makemessages --locale=de --extension xhtml

用逗号分隔多个扩展名或多次使用 -e--extension

django-admin makemessages --locale=de --extension=html,txt --extension xml

指定要处理的 locale。

指定要从处理中排除的区域设置。 如果未提供,则不排除任何区域设置。

用法示例:

django-admin makemessages --locale=pt_BR
django-admin makemessages --locale=pt_BR --locale=fr
django-admin makemessages -l pt_BR
django-admin makemessages -l pt_BR -l fr
django-admin makemessages --exclude=pt_BR
django-admin makemessages --exclude=pt_BR --exclude=fr
django-admin makemessages -x pt_BR
django-admin makemessages -x pt_BR -x fr

指定消息文件的域。 支持的选项有:

  • django 适用于所有 *.py*.html*.txt 文件(默认)
  • djangojs 用于 *.js 文件

在寻找新的翻译字符串时,跟踪指向目录的符号链接。

用法示例:

django-admin makemessages --locale=de --symlinks

忽略与给定 glob 样式模式匹配的文件或目录。 多次使用以忽略更多。

默认使用这些模式:'CVS''.*''*~''*.pyc'

用法示例:

django-admin makemessages --locale=en_US --ignore=apps/* --ignore=secret/*.html

禁用 --ignore 的默认值。

禁用将语言文件中的长消息行分成几行。

禁止在语言文件中写入“#: filename:line”注释行。 使用此选项会使技术熟练的翻译人员更难理解每条消息的上下文。

控制语言文件中的 #: filename:line 注释行。 如果选项是:

  • full(如果没有给出默认值):行包括文件名和行号。
  • file:行号省略。
  • never:行被抑制(同--no-location)。

需要 gettext 0.19 或更新版本。

防止删除在创建 .po 文件之前生成的临时 .pot 文件。 这对于调试可能阻止创建最终语言文件的错误很有用。

也可以看看

有关如何自定义 :djadmin:`makemessages` 传递给 xgettext 的关键字的说明,请参阅 自定义 makemessages 命令


makemigrations

根据检测到的模型更改创建新迁移。 迁移文档 中详细介绍了迁移及其与应用程序的关系等。

提供一个或多个应用程序名称作为参数将限制为指定的应用程序创建的迁移以及所需的任何依赖项(例如 ForeignKey 另一端的表)。

要将迁移添加到没有 migrations 目录的应用程序,请使用应用程序的 app_label 运行 makemigrations

禁止所有用户提示。 如果无法自动解决被抑制的提示,该命令将退出并显示错误代码 3。

输出指定应用程序的空迁移,以进行手动编辑。 这适用于高级用户,除非您熟悉迁移格式、迁移操作以及迁移之间的依赖关系,否则不应使用。

显示将在不实际将任何迁移文件写入磁盘的情况下进行的迁移。 将此选项与 --verbosity 3 一起使用还将显示将写入的完整迁移文件。

可以解决迁移冲突。

允许命名生成的迁移而不是使用生成的名称。 名称必须是有效的 Python 标识符

2.2 版中的新功能。


生成没有 Django 版本和时间戳头的迁移文件。

当检测到没有迁移的模型更改时,使 makemigrations 以非零状态退出。


migrate

将数据库状态与当前的模型和迁移集同步。 迁移文档 中详细介绍了迁移及其与应用程序的关系等。

该命令的行为根据提供的参数而改变:

  • 没有参数:运行所有的应用程序的所有迁移。
  • <app_label>:指定的应用程序运行其迁移,直到最近的迁移。 由于依赖关系,这也可能涉及运行其他应用程序的迁移。
  • <app_label> <migrationname>:将数据库架构带到应用命名迁移的状态,但不会应用同一应用程序中的后续迁移。 如果您之前迁移过指定迁移,则这可能涉及取消应用迁移。 您可以使用迁移名称的前缀,例如 0001,只要它对于给定的应用程序名称是唯一的。 使用名称 zero 一路迁移回来即 恢复应用程序的所有应用迁移。

警告

取消应用迁移时,所有依赖的迁移也将取消应用,无论 <app_label>。 您可以使用 --plan 来检查哪些迁移将不被应用。


指定要迁移的数据库。 默认为 default

标记目标的迁移(按照上面的规则)已应用,但没有实际运行 SQL 来改变你的数据库架构。

这适用于高级用户在手动应用更改时直接操作当前迁移状态; 请注意,使用 --fake 存在将迁移状态表置于需要手动恢复以使迁移正确运行的状态的风险。

如果在该迁移中的所有 CreateModel 操作创建的具有所有模型名称的所有数据库表已经存在,则允许 Django 跳过应用程序的初始迁移。 此选项适用于首次针对预先存在使用迁移的数据库运行迁移时使用。 但是,此选项不会检查匹配表名称之外的匹配数据库架构,因此只有在您确信现有架构与初始迁移中记录的架构匹配时才可以安全使用。

2.2 版中的新功能。


显示将为给定的 migrate 命令执行的迁移操作。

允许为应用程序创建表而无需迁移。 虽然不建议这样做,但迁移框架有时在具有数百个模型的大型项目上太慢。

禁止所有用户提示。 一个示例提示是询问删除陈旧的内容类型。


runserver

在本地机器上启动轻量级开发 Web 服务器。 默认情况下,服务器在 IP 地址 127.0.0.1 上的端口 8000 上运行。 您可以明确传入 IP 地址和端口号。

如果您以具有普通权限(推荐)的用户身份运行此脚本,您可能无权在低端口号上启动端口。 为超级用户 (root) 保留低端口号。

此服务器使用由 :setting:`WSGI_APPLICATION` 设置指定的 WSGI 应用程序对象。

请勿在生产环境中使用此服务器。 它没有经过安全审计或性能测试。 (这就是它会留下来的方式。 我们的工作是制作 Web 框架,而不是 Web 服务器,因此改进此服务器以处理生产环境超出了 Django 的范围。)

开发服务器根据需要自动为每个请求重新加载 Python 代码。 您无需重新启动服务器即可使代码更改生效。 但是,某些操作(例如添加文件)不会触发重新启动,因此在这些情况下您必须重新启动服务器。

如果您使用 Linux 或 MacOS 并安装 pywatchmanWatchman 服务,内核信号将用于自动重新加载服务器(而不是每秒轮询文件修改时间戳)。 这为大型项目提供了更好的性能、缩短了代码更改后的响应时间、更强大的更改检测并降低了功耗。

有许多文件的大目录可能会导致性能问题。

将 Watchman 用于包含大型非 Python 目录(如 node_modules)的项目时,建议忽略此目录以获得最佳性能。 有关如何执行此操作的信息,请参阅 watchman 文档


Watchman 超时

Watchman 客户端的默认超时时间为 5 秒。 您可以通过设置 DJANGO_WATCHMAN_TIMEOUT 环境变量来更改它。


2.2 版更改:Watchman 支持取代了对 pyinotify 的支持。


当您启动服务器时,每次在服务器运行时更改 Python 代码时,系统检查框架都会检查您的整个 Django 项目是否存在一些常见错误(请参阅 :djadmin:`check` 命令) . 如果发现任何错误,它们将打印到标准输出。

您可以根据需要运行任意数量的并发服务器,只要它们位于不同的端口上即可。 只需多次执行 django-admin runserver

请注意,网络上的其他机器无法访问默认 IP 地址 127.0.0.1。 要使网络上的其他机器可以查看您的开发服务器,请使用其自己的 IP 地址(例如 192.168.2.1) 或 0.0.0.0::(启用 IPv6)。

您可以提供一个用括号括起来的 IPv6 地址(例如 [200a::1]:8000)。 这将自动启用 IPv6 支持。

也可以使用只包含 ASCII 字符的主机名。

如果启用了 staticfiles contrib 应用程序(新项目中的默认设置),则 :djadmin:`runserver` 命令将被其自己的 runserver 命令覆盖。

服务器的每个请求和响应的日志发送到 django.server 记录器。

禁用自动重新加载器。 这意味着您在服务器运行时所做的任何 Python 代码更改将 不会 生效,如果特定的 Python 模块已经加载到内存中。

在开发服务器中禁用线程。 服务器默认是多线程的。

对开发服务器使用 IPv6。 这会将默认 IP 地址从 127.0.0.1 更改为 ::1

使用不同端口和地址的例子

IP 地址 127.0.0.1 上的端口 8000:

django-admin runserver

IP 地址 1.2.3.4 上的端口 8000:

django-admin runserver 1.2.3.4:8000

IP 地址 127.0.0.1 上的端口 7000:

django-admin runserver 7000

IP 地址 1.2.3.4 上的端口 7000:

django-admin runserver 1.2.3.4:7000

IPv6 地址 ::1 上的端口 8000:

django-admin runserver -6

IPv6 地址 ::1 上的端口 7000:

django-admin runserver -6 7000

IPv6 地址 2001:0db8:1234:5678::9 上的端口 7000:

django-admin runserver [2001:0db8:1234:5678::9]:7000

主机 localhost 的 IPv4 地址上的端口 8000:

django-admin runserver localhost:8000

主机 localhost 的 IPv6 地址上的端口 8000:

django-admin runserver -6 localhost:8000

用开发服务器服务静态文件

默认情况下,开发服务器不会为您的站点提供任何静态文件(例如 CSS 文件、图像、:setting:`MEDIA_URL` 下的内容等)。 如果要配置 Django 以提供静态媒体服务,请阅读管理静态文件(例如 图片、JavaScript、CSS) .


sendtestemail

向指定的收件人发送测试电子邮件(以确认通过 Django 发送的电子邮件正在工作)。 例如:

django-admin sendtestemail foo@example.com bar@example.com

有几个选项,你可以将它们任意组合在一起使用:

使用 mail_managers() 邮寄 :setting:`MANAGERS` 中指定的电子邮件地址。

使用 mail_admins() 邮寄 :setting:`ADMINS` 中指定的电子邮件地址。


shell

启动 Python 交互式解释器。

指定要使用的外壳。 默认情况下,如果安装了 IPythonbpython,Django 将使用。 如果两者都安装了,请指定您想要的那个:

蟒蛇:

django-admin shell -i ipython

蟒蛇:

django-admin shell -i bpython

如果您安装了“丰富的”shell,但想强制使用“普通”的 Python 解释器,请使用 python 作为接口名称,如下所示:

django-admin shell -i python

禁止读取“普通”Python 解释器的启动脚本。 默认情况下,读取 PYTHONSTARTUP 环境变量或 ~/.pythonrc.py 脚本指向的脚本。

允许您将命令作为字符串传递以作为 Django 执行它,如下所示:

django-admin shell --command="import django; print(django.__version__)"

您还可以在标准输入上传递代码以执行它。 例如:

$ django-admin shell <<EOF
> import django
> print(django.__version__)
> EOF

在 Windows 上,由于该平台上 select.select() 的实现限制,会输出 REPL。


showmigrations

显示项目中的所有迁移。 您可以选择以下两种格式之一:

列出 Django 知道的所有应用程序、每个应用程序可用的迁移以及是否应用了每个迁移(由迁移名称旁边的 [X] 标记)。

还列出了没有迁移的应用程序,但在它们下面印有 (no migrations)

这是默认的输出格式。

显示 Django 将遵循的迁移计划来应用迁移。 与 --list 一样,已应用的迁移由 [X] 标记。 对于 2 及以上的 --verbosity,还将显示迁移的所有依赖项。

app_label 的参数限制了输出,但是,也可能包含提供的应用程序的依赖项。

指定要检查的数据库。 默认为 default


sqlflush

打印将为 :djadmin:`flush` 命令执行的 SQL 语句。

指定要为其打印 SQL 的数据库。 默认为 default


sqlmigrate

打印指定迁移的 SQL。 这需要一个活动的数据库连接,它将用于解析约束名称; 这意味着您必须针对您希望稍后应用的数据库副本生成 SQL。

请注意, sqlmigrate 不会为其输出着色。

生成用于取消应用迁移的 SQL。 默认情况下,创建的 SQL 用于运行正向迁移。

指定要为其生成 SQL 的数据库。 默认为 default


sqlsequencereset

打印用于重置给定应用名称序列的 SQL 语句。

序列是一些数据库引擎用来跟踪自动递增字段的下一个可用数字的索引。

使用此命令生成 SQL,它将修复序列与其自动递增的字段数据不同步的情况。

指定要为其打印 SQL 的数据库。 默认为 default


squashmigrations

如果可能,将 app_label 上至并包括 migration_name 的迁移压缩为更少的迁移。 由此产生的被压扁的迁移可以安全地与未被压扁的迁移一起生活。 有关更多信息,请阅读 压缩迁移

当给出 start_migration_name 时,Django 将只包含从此迁移开始并包含此迁移的迁移。 这有助于减轻 RunPythondjango.db.migrations.operations.RunSQL 迁移操作的压缩限制。

在生成压缩迁移时禁用优化器。 默认情况下,Django 会尝试优化迁移中的操作以减小结果文件的大小。 如果此过程失败或创建不正确的迁移,请使用此选项,但也请提交有关该行为的 Django 错误报告,因为优化是安全的。

禁止所有的用户提示。

设置压缩迁移的名称。 省略时,名称基于第一次和最后一次迁移,中间有 _squashed_

2.2 版中的新功能。


生成没有 Django 版本和时间戳头的压缩迁移文件。


startapp

在当前目录或给定的目标目录中为给定的应用名创建一个 Django 应用目录结构。

默认情况下, :source:`新目录 ` 包含一个models.py文件和其他应用程序模板文件。 如果只给出了应用程序名称,应用程序目录将在当前工作目录中创建。

如果提供了可选目标,Django 将使用现有目录而不是创建一个新目录。 您可以使用 '。' 表示当前工作目录。

例如:

django-admin startapp myapp /Users/jezdez/Code/myapp

提供自定义应用程序模板文件的目录路径或压缩文件的路径(.tar.gz.tar.bz2.tgz.tbz.zip) 包含应用程序模板文件。

例如,这将在创建 myapp 应用程序时在给定目录中查找应用程序模板:

django-admin startapp --template=/Users/jezdez/Code/my_app_template myapp

Django 还将接受 URL(httphttpsftp)到带有应用程序模板文件的压缩档案,即时下载和提取它们。

例如,利用 GitHub 的功能将存储库公开为 zip 文件,您可以使用如下 URL:

django-admin startapp --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp

指定应使用模板引擎呈现应用模板中的哪些文件扩展名。 默认为 py

指定应使用模板引擎呈现应用模板中的哪些文件(除了那些匹配的 --extension)。 默认为空列表。

用于所有匹配文件的 模板上下文 是:

  • 传递给 startapp 命令的任何选项(在命令支持的选项中)
  • app_name – 传递给命令的应用程序名称
  • app_directory – 新创建的应用程序的完整路径
  • camel_case_app_name – 驼峰格式的应用名称
  • docs_version – 文档版本:'dev''1.x'
  • django_version – Django 的版本,例如 '2.0.3'

警告

当应用模板文件使用 Django 模板引擎(默认所有 *.py 文件)呈现时,Django 也会替换包含的所有杂散模板变量。 例如,如果其中一个 Python 文件包含解释与模板渲染相关的特定功能的文档字符串,则可能会导致示例不正确。

要解决此问题,您可以使用 :ttag:`templatetag` 模板标签来“转义”模板语法的各个部分。

此外,为了允许包含 Django 模板语言语法的 Python 模板文件同时防止打包系统尝试字节编译无效的 *.py 文件,以 .py-tpl 结尾的模板文件将重命名为 .py


startproject

在当前目录或给定的目标目录中为给定的项目名称创建一个 Django 项目目录结构。

默认情况下, :source:`新目录 ` 包含manage.py和一个项目包(包含一个settings.py和其他文件)。

如果只给出项目名称,项目目录和项目包都将命名为<projectname>,项目目录将在当前工作目录中创建。

如果提供了可选目标,Django 将使用该现有目录作为项目目录,并在其中创建 manage.py 和项目包。 用 '。' 表示当前工作目录。

例如:

django-admin startproject myproject /Users/jezdez/Code/myproject_repo

指定自定义项目模板的目录、文件路径或 URL。 有关示例和用法,请参阅 startapp --template 文档。

指定应使用模板引擎呈现项目模板中的哪些文件扩展名。 默认为 py

指定应使用模板引擎呈现项目模板中的哪些文件(除了那些匹配的 --extension)。 默认为空列表。

使用的 模板上下文 是:

  • 传递给 startproject 命令的任何选项(在命令支持的选项中)
  • project_name – 传递给命令的项目名称
  • project_directory – 新建项目的完整路径
  • secret_key:setting:`SECRET_KEY` 设置的随机密钥
  • docs_version – 文档版本:'dev''1.x'
  • django_version – Django 的版本,例如 '2.0.3'

另请参阅 :djadmin:`startapp` 中提到的 渲染警告


test

对所有已安装的应用程序运行测试。 有关更多信息,请参阅 在 Django 中进行测试

测试失败后立即停止运行测试并报告失败。

控制用于执行测试的测试运行器类。 此值覆盖 :setting:`TEST_RUNNER` 设置提供的值。

禁止所有用户提示。 典型的提示是关于删除现有测试数据库的警告。

测试运行器选项

test 命令代表指定的 --testrunner 接收选项。 这些是默认测试运行器的选项:DiscoverRunner

在测试运行之间保留测试数据库。 这具有跳过创建和销毁操作的优点,这可以大大减少运行测试的时间,尤其是在大型测试套件中的测试。 如果测试数据库不存在,它将在第一次运行时创建,然后为每次后续运行保留。 在运行测试套件之前,任何未应用的迁移也将应用于测试数据库。

以相反的执行顺序对测试用例进行排序。 这可能有助于调试未正确隔离的测试的副作用。 使用该选项时,按测试类分组 被保留。

在运行测试之前将 :setting:`DEBUG` 设置设置为 True。 这可能有助于对测试失败进行故障排除。

为失败的测试启用 SQL 日志记录 。 如果 --verbosity2,则还会输出通过测试的查询。

在单独的并行进程中运行测试。 由于现代处理器具有多个内核,因此可以显着加快测试运行速度。

默认情况下,--parallel 根据 multiprocessing.cpu_count() 每个内核运行一个进程。 您可以通过提供它作为选项的值来调整进程数,例如 --parallel=4,或通过设置 DJANGO_TEST_PROCESSES 环境变量。

Django 将测试用例——unittest.TestCase 子类——分发到子进程。 如果测试用例比配置的进程少,Django 会相应地减少进程数。

每个进程都有自己的数据库。 您必须确保不同的测试用例不会访问相同的资源。 例如,接触文件系统的测试用例应该创建一个临时目录供他们自己使用。

笔记

如果您有无法并行运行的测试类,您可以使用 SerializeMixin 顺序运行它们。 请参阅 强制按顺序运行测试类


此选项需要第三方 tblib 包才能正确显示回溯:

$ pip install tblib

此功能在 Windows 上不可用。 它也不适用于 Oracle 数据库后端。

如果要在调试测试时使用 pdb,则必须禁用并行执行 (--parallel=1)。 如果你不这样做,你会看到类似 bdb.BdbQuit 的东西。

警告

当启用测试并行化并且测试失败时,Django 可能无法显示异常回溯。 这会使调试变得困难。 如果遇到此问题,请在没有并行化的情况下运行受影响的测试以查看失败的回溯。

这是一个已知的限制。 它源于需要序列化对象以便在进程之间交换它们。 详见 python:pickle-picklable


--tag TAGS

仅运行标有指定标签 的测试 。 可以多次指定并与 test --exclude-tag 结合使用。

--exclude-tag EXCLUDE_TAGS

排除用指定标签标记的测试。 可以多次指定并与 test --tag 结合使用。


testserver

使用来自给定装置的数据运行 Django 开发服务器(如 :djadmin:`runserver`)。

例如,这个命令:

django-admin testserver mydata.json

...将执行以下步骤:

  1. 创建一个测试数据库,如测试数据库中所述。
  2. 使用来自给定夹具的夹具数据填充测试数据库。 (有关装置的更多信息,请参阅上面的 :djadmin:`loaddata` 文档。)
  3. 运行 Django 开发服务器(如 :djadmin:`runserver`),指向这个新创建的测试数据库而不是你的生产数据库。

这在很多方面都很有用:

  • 当您编写 单元测试 以了解您的视图如何处理某些夹具数据时,您可以手动使用 testserver 与 Web 浏览器中的视图交互。
  • 假设您正在开发您的 Django 应用程序,并且拥有您想要与之交互的数据库的“原始”副本。 您可以将数据库转储到夹具(使用 :djadmin:`dumpdata` 命令,如上所述),然后使用 testserver 使用该数据运行 Web 应用程序。 通过这种安排,您可以灵活地以任何方式弄乱您的数据,因为您知道所做的任何数据更改都只会对测试数据库进行。

请注意,此服务器 不会 自动检测 Python 源代码的更改(如 :djadmin:`runserver` 所做的那样)。 但是,它确实会检测对模板的更改。

指定与默认值 127.0.0.1:8000 不同的端口或 IP 地址和端口。 该值遵循与 :djadmin:`runserver` 命令的参数完全相同的格式并提供完全相同的功能。

实际案例

使用 fixture1fixture2 在端口 7000 上运行测试服务器:

django-admin testserver --addrport 7000 fixture1 fixture2
django-admin testserver fixture1 fixture2 --addrport 7000

(以上陈述是等价的。 我们将它们都包括在内,以证明选项是在夹具参数之前还是之后都无关紧要。)

使用 test 夹具在 1.2.3.4:7000 上运行:

django-admin testserver --addrport 1.2.3.4:7000 test

禁止所有用户提示。 典型的提示是关于删除现有测试数据库的警告。


应用程序提供的命令

某些命令仅在django.contrib应用程序工具他们已经 :设置:`启用 ` . 本节按应用对它们进行分组。

django.contrib.auth

changepassword

此命令仅在安装了 Django 的 身份验证系统 (django.contrib.auth) 时可用。

允许更改用户的密码。 它会提示您为给定用户输入两次新密码。 如果条目相同,这将立即成为新密码。 如果您不提供用户,该命令将尝试更改用户名与当前用户匹配的密码。

指定要为用户查询的数据库。 默认为 default

用法示例:

django-admin changepassword ringo

createsuperuser

此命令仅在安装了 Django 的 身份验证系统 (django.contrib.auth) 时可用。

创建超级用户帐户(拥有所有权限的用户)。 如果您需要创建初始超级用户帐户或需要以编程方式为您的站点生成超级用户帐户,这将非常有用。

以交互方式运行时,此命令将提示输入新超级用户帐户的密码。 以非交互方式运行时,不会设置密码,并且超级用户帐户将无法登录,直到手动为其设置密码。

禁止所有用户提示。 如果无法自动解决被抑制的提示,该命令将以错误代码 1 退出。

可以使用命令行上的 --username--email 参数提供新帐户的用户名和电子邮件地址。 如果未提供其中任何一个,createsuperuser 将在交互运行时提示输入。

指定保存超级用户对象的数据库。

如果您想自定义数据输入和验证,您可以子类化管理命令并覆盖 get_input_data()。 有关现有实现和方法参数的详细信息,请参阅源代码。 例如,如果您在 REQUIRED_FIELDS 中有一个 ForeignKey 并且希望允许创建实例而不是输入现有实例的主键,则它可能很有用。


django.contrib.contenttypes

remove_stale_contenttypes

此命令仅在安装了 Django 的 contenttypes app (django.contrib.contenttypes) 时可用。

删除数据库中过时的内容类型(从已删除的模型中)。 任何依赖于已删除内容类型的对象也将被删除。 在您确认可以继续删除之前,将显示已删除对象的列表。

指定要使用的数据库。 默认为 default


django.contrib.gis

ogrinspect

此命令仅在安装了 GeoDjango (django.contrib.gis) 时可用。

请参考其 :djadmin:`说明 ` 在 GeoDjango 文档中。


django.contrib.sessions

clearsessions

可以以定时任务的形式运行,也可以直接清理过期会话。


django.contrib.sitemaps

ping_google

此命令仅在安装了 站点地图框架 (django.contrib.sitemaps) 时可用。

请参考其 :djadmin:`说明 ` 在站点地图文档中。


django.contrib.staticfiles

collectstatic

此命令仅在安装了 静态文件应用程序 (django.contrib.staticfiles) 时可用。

请参考其 :djadmin:`说明 ` 在里面静态文件文档。


findstatic

此命令仅在安装了 静态文件应用程序 (django.contrib.staticfiles) 时可用。

请参考其 :djadmin:`说明 ` 在里面静态文件文档。


默认选项

虽然有些命令可能允许自己的自定义选项,但每个命令都允许以下选项:

将给定的文件系统路径添加到 Python 导入搜索路径 。 如果未提供,django-admin 将使用 PYTHONPATH 环境变量。

这个选项在 manage.py 中是不必要的,因为它负责为你设置 Python 路径。

用法示例:

django-admin migrate --pythonpath='/home/djangoprojects/myproject'

指定要使用的设置模块。 设置模块应该采用 Python 包语法,例如 mysite.settings。 如果未提供,django-admin 将使用 DJANGO_SETTINGS_MODULE 环境变量。

manage.py 中不需要此选项,因为它默认使用当前项目中的 settings.py

用法示例:

django-admin migrate --settings=mysite.settings

引发 CommandError 时显示完整的堆栈跟踪。 默认情况下,当 CommandError 发生时,django-admin 将显示一条简单的错误消息,以及任何其他异常的完整堆栈跟踪。

用法示例:

django-admin migrate --traceback

指定命令应打印到控制台的通知和调试信息的数量。

  • 0 表示无输出。
  • 1 表示正常输出(默认)。
  • 2 表示详细输出。
  • 3 表示 very 详细输出。

用法示例:

django-admin migrate --verbosity 2

禁用彩色命令输出。 一些命令将其输出格式化为彩色。 例如,错误将以红色打印到控制台,SQL 语句将以语法高亮显示。

用法示例:

django-admin runserver --no-color

2.2 版中的新功能。


如果命令输出被禁用,如 语法着色 中所述,则强制对其进行着色。 例如,您可能希望将彩色输出通过管道传送到另一个命令。


额外的细节

语法着色

如果您的终端支持 ANSI 彩色输出,django-admin / manage.py 命令将使用漂亮的彩色编码输出。 除非使用 --force-color 选项,否则如果您将命令的输出通过管道传输到另一个程序,它将不会使用颜色代码。

在 Windows 下,本机控制台不支持 ANSI 转义序列,因此默认情况下没有颜色输出。 但是您可以安装 ANSICON 第三方工具,Django 命令将检测它的存在并利用其服务对输出进行着色,就像在基于 Unix 的平台上一样。

可以自定义用于语法突出显示的颜色。 Django 附带三个调色板:

  • dark,适用于黑底白字的终端。 这是默认调色板。
  • light,适用于白底黑字的终端。
  • nocolor,禁用语法高亮。

您可以通过设置 DJANGO_COLORS 环境变量来指定要使用的调色板来选择调色板。 例如,要在 Unix 或 OS/X BASH shell 下指定 light 调色板,您可以在命令提示符下运行以下命令:

export DJANGO_COLORS="light"

您还可以自定义使用的颜色。 Django 指定了许多使用颜色的角色:

  • error - 重大错误。
  • notice - 一个小错误。
  • success - 成功。
  • warning - 警告。
  • sql_field - SQL 中模型字段的名称。
  • sql_coltype - SQL 中模型字段的类型。
  • sql_keyword - SQL 关键字。
  • sql_table - SQL 中模型的名称。
  • http_info - 1XX HTTP 信息服务器响应。
  • http_success - 2XX HTTP 成功服务器响应。
  • http_not_modified - 304 HTTP 未修改服务器响应。
  • http_redirect - 304 以外的 3XX HTTP 重定向服务器响应。
  • http_not_found - 404 HTTP Not Found 服务器响应。
  • http_bad_request - 404 以外的 4XX HTTP 错误请求服务器响应。
  • http_server_error - 5XX HTTP 服务器错误响应。
  • migrate_heading - 迁移管理命令中的标题。
  • migrate_label - 迁移名称。

这些角色中的每一个都可以从以下列表中指定特定的前景和背景颜色:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

然后可以通过使用以下显示选项来修改这些颜色:

  • bold
  • underscore
  • blink
  • reverse
  • conceal

颜色规格遵循以下模式之一:

  • role=fg
  • role=fg/bg
  • role=fg,option,option
  • role=fg/bg,option,option

其中 role 是有效颜色角色的名称,fg 是前景色,bg 是背景色,每个 option 是其中一种颜色修改选项。 然后用分号分隔多个颜色规格。 例如:

export DJANGO_COLORS="error=yellow/blue,blink;notice=magenta"

将指定使用蓝色闪烁黄色显示错误,并使用洋红色显示通知。 所有其他颜色角色将保持不变。

还可以通过扩展基本调色板来指定颜色。 如果您将调色板名称放在颜色规范中,则该调色板隐含的所有颜色都将被加载。 所以:

export DJANGO_COLORS="light;error=yellow/blue,blink;notice=magenta"

将指定使用浅色调色板中的所有颜色, 除外 用于错误和通知的颜色,这些颜色将按指定被覆盖。


Bash 补全

如果您使用 Bash shell,请考虑安装 Django bash 完成脚本,该脚本位于 Django 源代码分发中的 extras/django_bash_completion 中。 它启用 django-adminmanage.py 命令的制表符补全,因此您可以,例如……

  • 输入 django-admin
  • 按 [TAB] 查看所有可用选项。
  • 输入 sql,然后输入 [TAB],以查看名称以 sql 开头的所有可用选项。

有关如何添加自定义操作,请参阅 编写自定义 django-admin 命令


从你的代码中运行管理命令

django.core.management.call_command(name, *args, **options)

要从代码中调用管理命令,请使用 call_command

name
要调用的命令的名称或命令对象。 除非测试需要对象,否则首选传递名称。
*args
命令接受的参数列表。 参数传递给参数解析器,因此您可以使用与在命令行上相同的样式。 例如,call_command('flush', '--verbosity=0')
**options
命令行上接受的命名选项。 选项被传递给命令而不触发参数解析器,这意味着您需要传递正确的类型。 例如,call_command('flush', verbosity=0)(零必须是整数而不是字符串)。

实际案例

from django.core import management
from django.core.management.commands import loaddata

management.call_command('flush', verbosity=0, interactive=False)
management.call_command('loaddata', 'test_data', verbosity=0)
management.call_command(loaddata.Command(), 'test_data', verbosity=0)

请注意,不带参数的命令选项作为关键字与 TrueFalse 一起传递,如您在上面的 interactive 选项中所见。

可以使用以下任一语法传递命名参数:

# Similar to the command line
management.call_command('dumpdata', '--natural-foreign')

# Named argument similar to the command line minus the initial dashes and
# with internal dashes replaced by underscores
management.call_command('dumpdata', natural_foreign=True)

# `use_natural_foreign_keys` is the option destination variable
management.call_command('dumpdata', use_natural_foreign_keys=True)

使用 call_command() 而不是 django-adminmanage.py 时,某些命令选项具有不同的名称。 例如,django-admin createsuperuser --no-input 转换为 call_command('createsuperuser', interactive=False)。 要查找用于 call_command() 的关键字参数名称,请检查传递给 parser.add_argument()dest 参数的命令源代码。

接受多个选项的命令选项会传递一个列表:

management.call_command('dumpdata', exclude=['contenttypes', 'auth'])

call_command()函数的返回值与命令的handle()方法的返回值相同。

输出重定向

请注意,您可以重定向标准输出和错误流,因为所有命令都支持 stdoutstderr 选项。 例如,你可以写:

with open('/path/to/command_output', 'w') as f:
    management.call_command('dumpdata', stdout=f)