How do Django Fixtures handle ManyToManyFields?

CMaury picture CMaury · Oct 5, 2011 · Viewed 8k times · Source

I'm trying to load in around 30k xml files from clinicaltrials.gov into a mySQL database, and the way I am handling multiple locations, keywords, etc. are in a separate model using ManyToManyFields.

The best way I've figured out is to read the data in using a fixture. So my question is, how do I handle the fields where the data is a pointer to another model?

I unfortunately don't know enough about how ManyToMany/ForeignKeys work, to be able to answer...

Thanks for the help, sample code below: __ represent the ManyToMany fields

{
    "pk": trial_id,
    "model": trials.trial,
    "fields": {
            "trial_id": trial_id,
            "brief_title": brief_title,
            "official_title": official_title,
            "brief_summary": brief_summary,
            "detailed_Description": detailed_description,
            "overall_status": overall_status,
            "phase": phase,
            "enrollment": enrollment,
            "study_type": study_type,
            "condition": _______________,
            "elligibility": elligibility,
            "Criteria": ______________,
            "overall_contact": _______________,
            "location": ___________,
            "lastchanged_date": lastchanged_date,
            "firstreceived_date": firstreceived_date,
            "keyword": __________,
            "condition_mesh": condition_mesh,
    }

}

Answer

Timmy O'Mahony picture Timmy O'Mahony · Oct 5, 2011

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)