On Thu, 2009-01-15 at 19:06 -0800, andrew_dunn wrote: > I realize this is probably a very simple question, but I've been > trying to figure it out for days and have had no success. > > I've got a data set with about 11,000 lines (CSV). I've written a > piece of Python code that parses it and defines each field with a > variable name. What I'd like to do is get this into the database I'm > using for my Django app.
Every data import situation is different, but the general pattern works like this: (1) Read in the data and convert it to a useful (for some value of "useful") data structure. Almost always, a dictionary is a good data structure to use here. (2) Create a model instance or instances from that data. It might be that there are related models involved, so more than one model is required, in which case, step (1) might involve creating more than one dictionary per input record. (3) Profit. The reason I'm suggesting using a dictionary as your data structure is so that you can make the keys match the model field names and then pass in the dictionary to the class constructor (using "**kwargs" form of calling). The code might look like this: for record in input_iterator(): data_dict = process_entry(record) MyModel.object.create(**data) Maybe process_entry() returns more than one dictionary if you need to create more than one instance and maybe you need to save instance 1 before using it as a foreign key in instance 2, but that sort of stuff is just details. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---