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. excel 如何快速实现绝对引用

    当函数输入完成并框选数据区域后,变成如上效果时,按“F4键”即可

  2. ios中Pldatabase的用法

    将PLDATABASE加入到工程 下载PLDatabase 的dmg文件 将PLDatabase的framework复制到工程根目录在工程中加入该framework使用该framework进行数据库操 ...

  3. iOS Main Thread Checker: UI API called on a background thread的解释

    Xcode打印栏出现如下警告: Main Thread Checker: UI API called on a background thread 这个是什么错误呢? 其实这并不一定是错误,也可以理解 ...

  4. iOS之ProtocolBuffer搭建

    一.环境安装:pb编译器的安装 1.从https://github.com/google/protobuf/releases下载protocolBuffer对应版本编译器包,比如目前的对应的objc最 ...

  5. IP首部格式[转载]

    TCP 传输首部是 IP首部,所以把IP首部格式 拿过来研究下,看IP首部解码过程:   来源:51CTO博客,地址:http://lihuan.blog.51cto.com/4391550/7999 ...

  6. kafka负载均衡相关资料收集(一)

    key为null时Kafka会将消息发送给哪个分区? 当你编写kafka Producer时, 会生成KeyedMessage对象. 1 KeyedMessage<K, V> keyedM ...

  7. ng-file-upload结合springMVC使用

    引入angular和ng-file-upload. 前端代码 Upload.upload({ url: 'upload', fields: {'username': 'zouroto'}, // ad ...

  8. ubuntu下安装配置minicom(解决默认的端口/dev/tty8,改不过来的问题)

    Minicom是linux下串口通信的软件,下面讲下ubuntu Minicom的安装和配置. 安装: sudo apt-get install minicom 或在新立得软件包管理器中搜索“mini ...

  9. 【struts2】Action的生命周期

    Struts2的Action的生命周期是:Struts2为每个请求都重新初始化一个Action的实例.可以稍微改造一下代码来验证一下. 给HelloWorldAction加上一个public无参的构造 ...

  10. xtrabackup-工作原理

    数据备份 xtrabackup是基于innodb的crash恢复功能之上的.它会拷贝innodb数据文件(这会导致数据不一致的),然后对文件执行crash恢复使其一致. 因为innodb维护了redo ...