{"title":"如何为模型提供初始数据","version":"4.2","locale":"zh-hans","docname":"howto/initial-data","url":"/zh-hans/4.2/howto/initial-data/","canonical":"https://djangodocs.dev/zh-hans/4.2/howto/initial-data/","summary":"在初次设置应用程序时，使用硬编码数据预填充数据库有时是很有用的。您可以使用迁移或固定数据来提供初始数据。","html":"<h1>如何为模型提供初始数据<a class=\"heading-anchor\" href=\"#how-to-provide-initial-data-for-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>在初次设置应用程序时，使用硬编码数据预填充数据库有时是很有用的。您可以使用迁移或固定数据来提供初始数据。</p>\n<section id=\"provide-initial-data-with-migrations\">\n<h2>使用迁移提供初始数据<a class=\"heading-anchor\" href=\"#provide-initial-data-with-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>要自动加载应用程序的初始数据，请创建一个 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/migrations/#data-migrations\"><span class=\"std std-ref\">数据迁移</span></a>。迁移会在设置测试数据库时运行，因此数据将在那里可用，但受到 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/testing/overview/#test-case-serialized-rollback\"><span class=\"std std-ref\">一些限制</span></a> 的影响。</p>\n</section>\n<section id=\"provide-data-with-fixtures\">\n<span id=\"initial-data-via-fixtures\"></span><h2>使用固定数据提供数据<a class=\"heading-anchor\" href=\"#provide-data-with-fixtures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>你还可以使用 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/db/fixtures/#fixtures-explanation\"><span class=\"std std-ref\">fixtures</span></a> 提供数据，但这些数据不会自动加载，除非你使用 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/testing/tools/#django.test.TransactionTestCase.fixtures\" title=\"django.test.TransactionTestCase.fixtures\"><code class=\"xref py py-attr docutils literal notranslate\">TransactionTestCase.fixtures</code></a>。</p>\n<p>固定内容是一个 Django 知道如何导入数据库的集合。若你已有一些可用数据，最直接的创建固定内容的方式是使用  <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py dumpdata</code></a> 命令。或者，你可以手写固定内容；固定数据能被写作 JSON，XML 或 YAML （要求已安装 <a class=\"reference external\" href=\"https://pyyaml.org/\">PyYAML</a>）文档。 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/serialization/\"><span class=\"doc\">序列化文档</span></a> 拥有更多这些支持的 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/serialization/#serialization-formats\"><span class=\"std std-ref\">序列化格式</span></a> 的细节信息。</p>\n<p>举个例子，这有一个固定内容，描述了一个 <code class=\"docutils literal notranslate\">Person</code> 模型写成 JSON 后的样子：</p>\n<div class=\"code-block\" data-language=\"js\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Js</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Js code\"><code><span class=\"p\">[</span>\n  <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;model&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;myapp.person&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;pk&quot;</span><span class=\"o\">:</span> <span class=\"mf\">1</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;fields&quot;</span><span class=\"o\">:</span> <span class=\"p\">{</span>\n      <span class=\"s2\">&quot;first_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;John&quot;</span><span class=\"p\">,</span>\n      <span class=\"s2\">&quot;last_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;Lennon&quot;</span>\n    <span class=\"p\">}</span>\n  <span class=\"p\">},</span>\n  <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;model&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;myapp.person&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;pk&quot;</span><span class=\"o\">:</span> <span class=\"mf\">2</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;fields&quot;</span><span class=\"o\">:</span> <span class=\"p\">{</span>\n      <span class=\"s2\">&quot;first_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;Paul&quot;</span><span class=\"p\">,</span>\n      <span class=\"s2\">&quot;last_name&quot;</span><span class=\"o\">:</span> <span class=\"s2\">&quot;McCartney&quot;</span>\n    <span class=\"p\">}</span>\n  <span class=\"p\">}</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>以下是一样的固定内容，YAML 格式：</p>\n<div class=\"code-block\" data-language=\"yaml\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">YAML</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"YAML code\"><code><span class=\"p p-Indicator\">-</span> <span class=\"nt\">model</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">myapp.person</span>\n  <span class=\"nt\">pk</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">1</span>\n  <span class=\"nt\">fields</span><span class=\"p\">:</span>\n    <span class=\"nt\">first_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">John</span>\n    <span class=\"nt\">last_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">Lennon</span>\n<span class=\"p p-Indicator\">-</span> <span class=\"nt\">model</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">myapp.person</span>\n  <span class=\"nt\">pk</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">2</span>\n  <span class=\"nt\">fields</span><span class=\"p\">:</span>\n    <span class=\"nt\">first_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">Paul</span>\n    <span class=\"nt\">last_name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">McCartney</span>\n</code></pre></div>\n<p>你会将该数据存入应用中的 <code class=\"docutils literal notranslate\">fixtures</code> 字典。</p>\n<p>你可以通过调用 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">manage.py loaddata</code></a> <code class=\"docutils literal notranslate\">&lt;fixturename&gt;</code> 来加载数据，其中 <code class=\"docutils literal notranslate\">&lt;fixturename&gt;</code> 是你创建的 fixture 文件的名称。每次运行 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</code></a> 时，数据将从 fixture 中读取并重新加载到数据库中。请注意，这意味着如果你更改了 fixture 创建的行之一，然后再次运行 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\">loaddata</code></a>，你将覆盖你所做的任何更改。</p>\n<section id=\"tell-django-where-to-look-for-fixture-files\">\n<h3>告诉 Django 在哪里查找固定数据文件<a class=\"heading-anchor\" href=\"#tell-django-where-to-look-for-fixture-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>默认情况下，Django 在每个应用程序内部查找 fixtures，因此命令 <code class=\"docutils literal notranslate\">loaddata sample</code> 将找到文件 <code class=\"docutils literal notranslate\">my_app/fixtures/sample.json</code>。这也适用于相对路径，因此 <code class=\"docutils literal notranslate\">loaddata my_app/sample</code> 将找到文件 <code class=\"docutils literal notranslate\">my_app/fixtures/my_app/sample.json</code>。</p>\n<p>Django 还会在 <a class=\"reference internal\" href=\"/zh-hans/4.2/ref/settings/#std-setting-FIXTURE_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\">FIXTURE_DIRS</code></a> 设置提供的目录列表中查找 fixtures。</p>\n<p>要完全阻止默认搜索发生，使用绝对路径来指定 fixture 文件的位置，例如 <code class=\"docutils literal notranslate\">loaddata /path/to/sample</code>。</p>\n<aside class=\"admonition-namespace-your-fixture-files admonition\" role=\"note\">\n<p class=\"admonition-title\">为你的固定数据文件设置命名空间</p>\n<p>Django 将使用它找到的第一个与名称匹配的 fixture 文件，因此如果在不同应用程序中有具有相同名称的 fixture 文件，你将无法在你的 <code class=\"docutils literal notranslate\">loaddata</code> 命令中区分它们。避免这个问题的最简单方法是通过为 fixture 文件添加 <em>命名空间</em>。也就是说，将它们放在以其应用程序命名的目录中，就像上面的相对路径示例中一样。</p>\n</aside>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>固定内容通常备 <a class=\"reference internal\" href=\"/zh-hans/4.2/topics/testing/tools/#topics-testing-fixtures\"><span class=\"std std-ref\">测试框架</span></a> 用于创建永久的测试环境。</p>\n</aside>\n</section>\n</section>","rootId":"how-to-provide-initial-data-for-models","toc":[{"title":"使用迁移提供初始数据","anchor":"provide-initial-data-with-migrations","children":[]},{"title":"使用固定数据提供数据","anchor":"provide-data-with-fixtures","children":[{"title":"告诉 Django 在哪里查找固定数据文件","anchor":"tell-django-where-to-look-for-fixture-files","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"操作指南","url":"/zh-hans/4.2/howto/"}],"prev":{"docname":"howto/error-reporting","title":"如何管理错误报告","url":"/zh-hans/4.2/howto/error-reporting/"},"next":{"docname":"howto/legacy-databases","title":"如何将 Django 与旧有的数据库集成","url":"/zh-hans/4.2/howto/legacy-databases/"},"formats":{"html":"/zh-hans/4.2/howto/initial-data/","markdown":"/zh-hans/4.2/howto/initial-data.md","json":"/zh-hans/4.2/howto/initial-data.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/howto/initial-data.txt","official":"https://docs.djangoproject.com/zh-hans/4.2/howto/initial-data/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}