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. http://blog.csdn.net/u011277123/article/details/53665302

    导读:Oracle中,除Exp命令可进行一般的全库.全用户导出外,还可通过增加相关参数条件实现对部分库表或数据的导出. 测试环境 在如下图的测试环境中,当前登录用户名为“jck”,该用户下有200多张 ...

  2. SpringCloud服务消费者第一次调用出现超时问题的解决方案

    在第一次访问服务消费者的时候(消费者去调用服务提供者服务)会出现如下异常: com.netflix.hystrix.exception.HystrixRuntimeException: TestSer ...

  3. 设置mysql group_concat长度

    #在MySQL配置文件(my.ini)中默认无该配置项,使用默认值时,值为1024,可在客户端执行下列语句修改: #SET GLOBAL group_concat_max_len = 1024; #该 ...

  4. ASP.NET HttpModule URL 重写 (一) 【Z】

    大家好,又来和大家见面了,此次给大家带来的URL重写,关于URL重写是什么,有什么好处,如何重写,今天我和大家一起分享一下我的经验 一.URL重写 URL重写就是首先获得一个进入的URL请求然后把它重 ...

  5. Scribes:小型文本编辑器,支持远程编辑

    Scribes是一款简洁的文本编辑器.现在给大家介绍下.     功能列表,  Python插件扩展  支持远程编辑(如ftp, sftp, ssh, samba, webdav, webdavs)  ...

  6. java 获取计算机名称, ip, mac地址

    写在前面 通常所说查询本机mac地址是以以太网为准的, 也就是网线那个口..这种描述略捞. 但是通过java的getHostAddress获取ip以及getHardwareAddress()方法获取m ...

  7. windows下如何生成gitlab ssh公钥

    1.查看是否已经有了ssh密钥:cd ~/.ssh如果没有密钥则不会有此文件夹,有则备份删除2.生存密钥: $ ssh-keygen -t rsa -C “你的邮箱”按3个回车,密码为空. Your ...

  8. android image加载中等待动画

    在布局中添加一个ImageViw和一个EditText. <ImageView android:id="@+id/loading_imageView_info" androi ...

  9. System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded

    昨天客户跟我说,突然一个页面频繁地报ThrowIfMaxHttpCollectionKeysExceeded这个异常.而且是数据量大的时候报错,数据量小的时候OK. 根据异常的名称也能看得差不多超过了 ...

  10. Mac Apache Maven 配置

    1.配置准备工作 1)配置 Maven 准备工作 下载相关软件 apache-maven-3.5.3.zip Maven 官网 Maven for Mac 配置软件下载地址,密码:q9u3. Mave ...