Search code examples
djangodjango-fixturesnatural-key

Django fixtures many to many with natural keys over multiple fields


I'm writing some additional fixtures for a project, and I have a question about how to use the natural keys.

In another fixture, the natural key for area is defined like so:

"fields": {
            "area": [
                "auckland",
                "NZ"
            ],

However, I'm writing a fixture for a model with a ManyToMany relation, so how do I include multiple two object keys? the following doesn't seem to work.

"fields": {
            "areas": [
                "auckland",
                "NZ",
                "sydney",
                "AUS"
            ],

Solution

  • Ok, turns out its super straightforward:

    "fields": {
                "areas": [
                    [
                        "auckland",
                        "NZ",
                    ],
                    [
                        "sydney",
                        "AUS"
                    ]
                ],