[Django]-How do Django Fixtures handle ManyToManyFields?

38πŸ‘

βœ…

A foreign key is simple the pk of the object you are linking to, a manytomanyfield uses a list of pk’s. so

[
    {
        "pk":1,
        "model":farm.fruit,
        "fields":{
            "name" : "Apple",
            "color" : "Green",
        }
    },
    {
        "pk":2,
        "model":farm.fruit,
        "fields":{
            "name" : "Orange",
            "color" : "Orange",
        }
    },
    {
         "pk":3,
         "model":person.farmer,
         "fields":{
             "name":"Bill",
             "favorite":1,
             "likes":[1,2],
         }
    }
]

You will need to probably write a conversion script to get this done. Fixtures can be very flimsy; it’s difficult to get the working so experiment with a subset before you spend a lot of time converting the 30k records (only to find they might not import)

Leave a comment