{"id":6359,"date":"2023-01-11T07:17:17","date_gmt":"2023-01-11T07:17:17","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=6359"},"modified":"2023-01-18T03:25:21","modified_gmt":"2023-01-18T03:25:21","slug":"django-dumpdata","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/django-tutorial\/django-dumpdata\/","title":{"rendered":"Django dumpdata"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Django <code>dumpdata<\/code> command to export the database into files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-django-dumpdata-command'>Introduction to the Django dumpdata command <a href=\"#introduction-to-the-django-dumpdata-command\" class=\"anchor\" id=\"introduction-to-the-django-dumpdata-command\" title=\"Anchor for Introduction to the Django dumpdata command\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to move some common data from a test database to the production database.<\/p>\n\n\n\n<p>To do that you use the <code>dumpdata<\/code> command to export data in the test database into a file and import it to the production database using the <code>loaddata<\/code> command.<\/p>\n\n\n\n<p>The <code>dumpdata<\/code> command has many options that allow you to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Export all model instances of all the apps (the whole database) into a file.<\/li>\n\n\n\n<li>Export all model instances of an app into a file.<\/li>\n\n\n\n<li>Export some model instances (some tables in the database) into a file.<\/li>\n<\/ul>\n\n\n\n<p>The format of the output file can be <code>xml<\/code>, <code>json<\/code>, <code>jsonl<\/code>, or <code>yaml<\/code>. The following dump the whole database into a <code>data.json<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py dumpdata &gt; data.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you open the <code>data.json<\/code> file, you&#8217;ll see lots of data. For example, the following shows the Employee&#8217;s instance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">...\n{\n    <span class=\"hljs-string\">\"model\"<\/span>: <span class=\"hljs-string\">\"hr.employee\"<\/span>,\n    <span class=\"hljs-string\">\"pk\"<\/span>: <span class=\"hljs-number\">5<\/span>,\n    <span class=\"hljs-string\">\"fields\"<\/span>: {\n      <span class=\"hljs-string\">\"first_name\"<\/span>: <span class=\"hljs-string\">\"John\"<\/span>,\n      <span class=\"hljs-string\">\"last_name\"<\/span>: <span class=\"hljs-string\">\"Doe\"<\/span>,\n      <span class=\"hljs-string\">\"contact\"<\/span>: null,\n      <span class=\"hljs-string\">\"department\"<\/span>: <span class=\"hljs-number\">1<\/span>,\n      <span class=\"hljs-string\">\"compensations\"<\/span>: &#91;<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>]\n    }\n  },\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The sample model contains the following information: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The model name (<code>hr.employee<\/code>).<\/li>\n\n\n\n<li>The primary key value (<code>pk<\/code>).<\/li>\n\n\n\n<li>The fields  (<code>first_name<\/code>, <code>last_name<\/code>, <code>contact<\/code>, <code>department<\/code>, and <code>compensations<\/code>) of the <code>Employee<\/code> model.<\/li>\n<\/ul>\n\n\n\n<p>If you want to export the whole database to another format like <code>XML<\/code>, you need to specify the format option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py dumpdata &gt; filename --format file_format<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>file_format<\/code> can be <code>json<\/code>, <code>jsonl<\/code>, <code>xml<\/code>, and <code>yaml<\/code>.<\/p>\n\n\n\n<p>For example, the following command exports the whole database into an <code>XML<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py dumpdata &gt; data.xml --format xml<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='exporting-data-from-a-specific-app'>Exporting data from a specific app <a href=\"#exporting-data-from-a-specific-app\" class=\"anchor\" id=\"exporting-data-from-a-specific-app\" title=\"Anchor for Exporting data from a specific app\">#<\/a><\/h2>\n\n\n\n<p>To export data of a specific app, you specify the app name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py dumpdata app_name &gt; filename.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following command exports the model instances of the <code>hr<\/code> app:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py dumpdata hr &gt; hr.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='exporting-data-from-a-specific-model'>Exporting data from a specific model <a href=\"#exporting-data-from-a-specific-model\" class=\"anchor\" id=\"exporting-data-from-a-specific-model\" title=\"Anchor for Exporting data from a specific model\">#<\/a><\/h2>\n\n\n\n<p>To dump the data of a specific table, you specify the app name and model name as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py app_name.model_name &gt; filename<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following command dumps all the instances of the Employee table in the <code>HR<\/code> application:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py dumpdata hr.employee &gt; hr_employee.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='exporting-data-by-excluding-one-or-more-models'>Exporting data by excluding one or more models <a href=\"#exporting-data-by-excluding-one-or-more-models\" class=\"anchor\" id=\"exporting-data-by-excluding-one-or-more-models\" title=\"Anchor for Exporting data by excluding one or more models\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you want to export data from all models except for one or more models. In this case, you can use the <code>--exclude<\/code> option:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py --exclude app_name.model_name &gt; filename<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the command may contain multiple <code>--exclude<\/code> options so that you can exclude multiple models.<\/p>\n\n\n\n<p>For example, the following export data from the whole database except for the contact model:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py --exclude hr.contact &gt; data.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following command exports data from the whole database except for the contact and department models:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python manage.py --exclude hr.contact --exclude department &gt; data.json<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Django <code>dumpdata<\/code> command to export data of one or more models.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"6359\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-dumpdata\/\"\n\t\t\t\tdata-post-title=\"Django dumpdata\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"6359\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/django-tutorial\/django-dumpdata\/\"\n\t\t\t\tdata-post-title=\"Django dumpdata\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Django dumpdata command to export the database into files.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":5531,"menu_order":27,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6359","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=6359"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/6359\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/5531"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=6359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}