You can try to exclude some types of data in the database when dumping data.
First, dump data:
python manage.py dumpdata --indent 4 > datadump.json
A few lines from this file for example:
[
{
"model": "admin.logentry",
"pk": 1,
"fields": {
"action_time": "2023-09-28T08:33:24.827Z",
"user": 1,
"content_type": 3,
"object_id": "1",
"object_repr": "user",
"action_flag": 1,
"change_message": "[{"added": {}}]"
} },
...
{
"model": "auth.permission",
"pk": 1,
"fields": {
"name": "Can add log entry",
"content_type": 1,
"codename": "add_logentry"
} },
...
{
"model": "auth.group",
"pk": 1,
"fields": {
"name": "user",
"permissions": [
25,
26,
27
]
} },
...
]
Try to search and see the text like this "model"
in the json dump file.
You can also find all types of content in the contenttype
model, a table for the this model is django_content_type
:
SELECT * FROM django_content_type ORDER BY id;
An example of data from this table:
+----+--------------+----------------+
| id | app_label | model |
+----+--------------+----------------+
| 1 | admin | logentry |
| 2 | auth | permission |
| 3 | auth | group |
| 4 | contenttypes | contenttype |
| 5 | sessions | session |
| 6 | user | user |
| 7 | user | historicaluser |
| 8 | constance | constance |
| 9 | tracking | pageview |
| 10 | tracking | visitor |
+----+--------------+----------------+
After you can exclude this type of data by adding several --exclude
to the manage.py dumpdata
For example:
- "model": "auth.permission", can be excluded like this:
--exclude auth.permission
. Using this option --exclude auth
you can exclude all models for application auth
, if there are such models, in this case these will be the permission
and the group
models.
- "model": "contenttypes.contenttype", can be excluded like this:
--exclude contenttypes.contenttype
.
- "model": "user.historicaluser", can be excluded like this:
--exclude user.historicaluser
.
- "model": "sessions.session", can be excluded like this:
--exclude sessions.session
or --exclude sessions
.
- "model": "tracking.pageview", can be excluded like this:
--exclude tracking.pageview
. If you use --exclude tracking
, both models tracking.pageview
and tracking.visitor
will be excluded.
The final code for 5 lines above:
python manage.py dumpdata --exclude auth.permission --exclude contenttypes --exclude user.historicaluser --exclude sessions.session --exclude tracking --indent 4 > dumpdata.json
You can also combine the dump script with --natural-foreign
and --natural-primary
options, read more here: https://docs.djangoproject.com/en/5.0/topics/serialization/#natural-keys.