Django dumpdata and loaddata

dumpdata command

  • It is a django management command, which can be use to backup(export) you model instances or whole database

dumpdata for basic database dump

  • Following command will dump whole database in to a db.json file
./manage.py dumpdata > db.json

dumpdata for backup specific app

  • Following command will dump the content in django admin app into admin.json file
./manage.py dumpdata admin > admin.json

dumpdata for backup specific table

  • Following command will dump only the content in django admin.logentry table
./manage.py dumpdata admin.logentry > logentry.json
  • Following command will dump the content in django auth.user table
./manage.py dumpdata auth.user > user.json

dumpdata (--exclude)

  • You can use --exclude option to specify apps/tables which don't need being dumped

  • Following command will dump the whole database with out including auth.permission table content

./manage.py dumpdata --exclude auth.permission > db.json

dumpdata (--indent)

  • By default, dumpdata will output all data on a single line. It isn’t easy for humans to read

  • You can use the --indent option to pretty-print the output with a number of indentation spaces

./manage.py dumpdata auth.user --indent 2 > user.json
  • Example output of above command is below

dumpdata (--format)

  • By default, dumpdata will format its output in JSON

  • You can specify the format using --format option

  • Command supports for following formats(serialization formats)

  1. json
  2. xml
  3. yaml
./manage.py dumpdata auth.user --indent 2 --format xml > user.xml
  • Above command output an xml file(user.xml)

loaddata command

  • This command can be use to load the fixtures(database dumps) into database
./manage.py loaddata user.json
  • This command will add the user.json file content into the database

Restore fresh database

  • When you backup whole database by using dumpdata command, it will backup all the database tables

  • If you use this database dump to load the fresh database(in another django project), it can be causes IntegrityError (If you loaddata in same database it works fine)

  • To fix this problem, make sure to backup the database by excluding contenttypes and auth.permissions tables

./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
  • Now you can use loaddata command with a fresh database
./manage.py loaddata db.json

 

 

Django使用manage.py备份与恢复数据的更多相关文章

  1. Django笔记 manage.py脚本的使用

    1. 管理Django项目 python manage.py startproject projectname # 新建Django project ~~projectname是自己的项目名称 pyt ...

  2. python后台架构Django教程——manage.py命令

    一.manage.py命令选项 manage.py是每个Django项目中自动生成的一个用于管理项目的脚本文件,需要通过python命令执行.manage.py接受的是Django提供的内置命令. 内 ...

  3. No module named _sqlite3 django python manage.py runserver

    linux 执行django(python manage.py runserver),报错No module named _sqlite3,需要安装sqlite-devel,再重新编译安装python ...

  4. django python manage.py runserver 流程

    python manage.py runserver 流程分析 版本 python27 django 1.0 搭建可运行的环境 创建python27 虚拟环境 github 下载 django-1.0 ...

  5. 关于django python manage.py startapp 应用名 出错异常原因

    如题,在控制台运行python manage.py startapp sales 建立一个应用报错异常 1.应用名不能包含下划线等字符 所以app-demo 不能作为应用名被定义 2.manage.p ...

  6. Django中manage.py migrate无效的问题

    在改变Django-model中结构后,makemigrations可以识别到改变但migrate没有操作,数据库中表结构也没有改变,原因如下: 在由Django-model自动生成的数据库表中有名为 ...

  7. Django manage.py 命令详解

    manage.py 查看命令的作用的语句 C:\Users\Administrator> python manage.py help Type 'manage.py help <subco ...

  8. django orm 分页(paginator)取数据出现警告manage.py:1: UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class 'sign.models.Guest'> QuerySet.

    使用django的orm做分页(Paginator)时出现了下面的警告 In [19]: p=Paginator(guest_list,2) manage.py:1: UnorderedObjectL ...

  9. Django学习之manage.py使用

    1.django-admin.py startproject mysite 开始一个项目,会初始化一些项目的结构文件 2.python manage.py runserver ip:port 如: p ...

随机推荐

  1. 【Linux】find命令

    用途 find命令用于在指定目录下查找文件. 全称 无 参数 -name :后跟需要匹配的文件名模式,需要使用引号引起来 下面是一些简单的示例查找:(~表示$HOME目录) 1.查找当前$HOME下' ...

  2. Ajax学习(二):模仿jQuery的Ajax封装工具

    通过上一节的学习,基本了解Ajax的使用, 但是这样使用很麻烦,这里封装ajax为一个方法,作为一个ajax工具,传入相应参数就可以实现ajax的使用. 模仿jQuery的Ajax. 如下是jQuer ...

  3. 转 linux下面apache2.0.52+php5+gd2+mysql

    gd2才开始支持真彩图片的创建,所以,,升级服务器,因为原来的安装都是默认的系统安装,也更因为是个菜鸟,所以,安装很困难,起初根据网上一些文章在我的red hat A 3 上安装测试,不过,测试了安装 ...

  4. java第六节 字符串/集合

    /* *String类和StringBuffer类 * 位于java.lang包中 * String类对象中的内容一旦被初始化就不能再改变 * StringBuffer类中用于封装内容可以改变的字符串 ...

  5. 9273:PKU2506Tiling

    9273:PKU2506Tiling 来源:http://noi.openjudge.cn/ch0202/9273/ 总时间限制:2000ms  单个测试点时间限制:1000ms内存限制:131072 ...

  6. Ubuntu下的init.d管理update-rc.d

    计算机在启动的时候会自动执行一些脚本,用于启动一些应用程序服务,update-rc.d 是管理这些脚本的常用命令之一. 首先这是 LinuxQuestions 中对 update-rc.d 的定义: ...

  7. MessageListActivity has leaked IntentReceiver

    1. 在MessagelistActivity中出现has leaked IntentReceiver的异常.异常日志如下. 07-15 08:09:53.211: E/ActivityThread( ...

  8. webDriver.Close() 和 webDriver.Quit() 、webDriver.Dispose() 的区别

    1. webDriver.Close() - Close the browser window that the driver has focus of //关闭当前焦点所在的窗口2. webDriv ...

  9. iOS 用自签名证书实现 HTTPS 请求的原理

    在16年的WWDC中,Apple已表示将从2017年1月1日起,所有新提交的App必须强制性应用HTTPS协议来进行网络请求.默认情况下非HTTPS的网络访问是禁止的并且不能再通过简单粗暴的向Info ...

  10. Linux shell命令:用 !$ 防止误操作

    shell 的通配符匹配非常强大,不过也非常危险,不少同学都碰到这样的问题,比如 rm a*,结果一个手抖,a 和星号之间多了个空格,结果目录下的文件都灰飞烟灭了…… bash 支持一个特殊的变量 ! ...