How do I change the format of the permissions field in the outputted fixture for the dumpdata command?

Viewed 126

When I create data fixtures for user groups using the dumpdata command:

python manage.py dumpdata auth.Group --indent 4 > fixtures.json

I get the ids of the permissions (23, 25, 26) in the following format:

{
    "model": "auth.group",
    "pk": 7,
    "fields": {
        "name": "Subscribers",
        "permissions": [
            23,
            25,
            26 
        ]
    }
}

How do I get it to be in the format below for the permissions field?

{
    "model": "auth.group",
    "pk": 7,
    "fields": {
        "name": "Subscribers",
        "permissions": [
            ["add_location", "main", "location"],
            ["change_location", "main", "location"],
            ["delete_location", "main", "location"]
        ]
    }
}

I looked up the documentation but couldn't find anything on this.

1 Answers

To use the natural key ((codename, app_label, model) is the natural key of the Permission model) of the foreign key models while serializing using the dumpdata command you need to use the --natural-foreign flag as described in the documentation:

python manage.py dumpdata auth.Group --natural-foreign --indent 4 > fixtures.json
Related