23

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

}

CMaury
  • 1,273
  • 5
  • 13
  • 25
  • 2
    I do not remember the answer, but if you run your development server, create sample data using admin, and then dump the database in to a json file using "manage.py dumpdata" command, you will be able to create your fixtures using the file as an example. that is what I did when I needed to find out how to create fixtures for relationship fields. :) – akonsu Oct 05 '11 at 04:31

1 Answers1

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)

Carlos Calla
  • 6,556
  • 2
  • 16
  • 23
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • Thanks. Given the flimsiness of fixtures, would you recommend another way of importing XML data into the DB? – CMaury Oct 05 '11 at 18:40
  • Use a Python script that reads the XML, and creates an Trial object, then just use trial.save(). – blaze Feb 06 '14 at 03:30