Load a dictionary into django model

46 Views Asked by At

I have a csv file that has multiple headers. I would like to get the data into a database using Django. The first token of each line indicates which type of event it is and identifying the table it needs to go in. I go through each line, matching up the headers and using a switch I’m ready to pass a correct dictionary to the model. I can’t finger out how to pass the dictionary into the model. I don’t want to go attribute by attribute because all the tables would be almost 10k lines of code. Is there a way to say

Event_{type}.objects.create(my_dict)
1

There are 1 best solutions below

2
Marco On

You can make use of the Python's list unpacking argument:

Event_{type}.objects.create(**my_dict)

Make sure your dict keys are matching the db column names.