dumpdata
命令:
它可以用来备份(导出)模型实例或整个数据库
1 | ./manage.py dumpdata --help |
-
基础数据库导出
1
./manage.py dumpdata > db.json
这会导出整个数据库到 db.json
-
备份指定的 app
1
./manage.py dumpdata admin > admin.json
这会导出 admin 应用的内容到 admin.json
-
备份指定的数据表
1
./manage.py dumpdata admin.logentry > logentry.json
这会导出 admin.logentry 数据表的所有数据
1
./manage.py dumpdata auth.user > user.json
这会导出 auth.user 数据表的所有数据
-
dumpdata —exclude
—exclude
选项用来指定无需被导出的 apps/tables1
./manage.py dumpdata --exclude auth.permission > db.json
这会导出整个数据库,但不包括 auth.permisson
-
dumpdata —intent
默认情况,
dumpdata
的输出会挤在同一行,可读性很差。使用 —indent 可以设定缩进美化输出1
./manage.py dumpdata auth.user --indent 2 > user.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38[
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "pbkdf2_sha256$150000$i8oET981EnSJ$d2RCpfY76gFHbwUs1HekSK+pOLYMJFcJ1wFcuyf6R28=",
"last_login": "2020-04-13T09:21:34.639Z",
"is_superuser": true,
"username": "xiao",
"first_name": "",
"last_name": "",
"email": "yuechuan.xiao@artosyn.cn",
"is_staff": true,
"is_active": true,
"date_joined": "2020-04-13T08:59:01.310Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 2,
"fields": {
"password": "pbkdf2_sha256$150000$PgBKh5sMAE1y$xdFkYi+gprF1v2rlOyw2OOsRn87zSeTVLJ9dGfoXzIw=",
"last_login": null,
"is_superuser": true,
"username": "qa",
"first_name": "",
"last_name": "",
"email": "qa@artosyn.cn",
"is_staff": true,
"is_active": true,
"date_joined": "2020-04-13T08:59:16.279Z",
"groups": [],
"user_permissions": []
}
}
] -
dumpdata —format
默认输出格式为 JSON。使用
—format
可以指定输出格式- json
- xml
- yaml
1
./manage.py dumpdata auth.user --indent 2 --format xml > user.xml
这会输出 xml 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<django-objects version="1.0">
<object model="auth.user" pk="1">
<field name="password" type="CharField">pbkdf2_sha256$150000$i8oET981EnSJ$d2RCpfY76gFHbwUs1HekSK+pOLYMJFcJ1wFcuyf6R28=</field>
<field name="last_login" type="DateTimeField">2020-04-13T09:21:34.639297+00:00</field>
<field name="is_superuser" type="BooleanField">True</field>
<field name="username" type="CharField">xiao</field>
<field name="first_name" type="CharField"></field>
<field name="last_name" type="CharField"></field>
<field name="email" type="CharField">yuechuan.xiao@artosyn.cn</field>
<field name="is_staff" type="BooleanField">True</field>
<field name="is_active" type="BooleanField">True</field>
<field name="date_joined" type="DateTimeField">2020-04-13T08:59:01.310568+00:00</field>
<field name="groups" rel="ManyToManyRel" to="auth.group"></field>
<field name="user_permissions" rel="ManyToManyRel" to="auth.permission"></field>
</object>
<object model="auth.user" pk="2">
<field name="password" type="CharField">pbkdf2_sha256$150000$PgBKh5sMAE1y$xdFkYi+gprF1v2rlOyw2OOsRn87zSeTVLJ9dGfoXzIw=</field>
<field name="last_login" type="DateTimeField"><None></None></field>
<field name="is_superuser" type="BooleanField">True</field>
<field name="username" type="CharField">qa</field>
<field name="first_name" type="CharField"></field>
<field name="last_name" type="CharField"></field>
<field name="email" type="CharField">qa@artosyn.cn</field>
<field name="is_staff" type="BooleanField">True</field>
<field name="is_active" type="BooleanField">True</field>
<field name="date_joined" type="DateTimeField">2020-04-13T08:59:16.279788+00:00</field>
<field name="groups" rel="ManyToManyRel" to="auth.group"></field>
<field name="user_permissions" rel="ManyToManyRel" to="auth.permission"></field>
</object>
</django-objects> -
loaddata
命令用来导入 fixtures(dumpdata 导出的数据)到数据库
1
./manage.py loaddata user.json
这会导入 user.json 里的内容到数据库
-
恢复 fresh database
当你通过 dumpdata 命令备份整个数据库时,它将备份所有数据表。若使用 dump 文件导入到另外的 Django 项目,会导致
IntegrityError
。可以通过备份时加入选项
—exclude
contenttypes 和 auth.permissions 数据表修复此问题1
./manage.py dumpdata --exclude auth.permission --exclude contenttypes > db.json
现在再用 loaddata 命令导入 fresh dababase
1
./manage.py loaddata db.json