Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Django Fixtures handle ManyToManyFields?

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,
    }

}

like image 995
CMaury Avatar asked Oct 05 '11 00:10

CMaury


People also ask

What is the purpose of Django fixtures?

A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command.

How do I create a fixture in Django?

You must create a directory in your app named fixtures and put your fixtures files there. You can write them in json or xml, one easy way to make them is to create some objects in the admin interface and then run manage.py dumpdata. That would dump the data from the objects you created into fixture files.


1 Answers

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)

like image 80
Timmy O'Mahony Avatar answered Oct 23 '22 05:10

Timmy O'Mahony