Bulk insert to my model on Django from a dictionary


on my models.py file i have defined some model with more than 20 attributes by their keywords. And, django automatically created a list on my database, with the column names of my model’s attributes.

All I ask is, I already opened a data file, splitted the data in them and added to a dictionary as key, value pairs. Now i need to reach out these key, value pairs and if the “key” is the same with the “attribute name” on my model, django should insert the “value” of this “key” to the appropriate, matched column name’s value part on my model’s table on DB. How can I do that?


Are you talking about the situation where you are processing a single update to a single row in the model? Or do you have multiple lines where each line is updating a different row?

How “secure” or “reliable” is the incoming data? Can you be sure that the keys match the model field names, or is there the possibility of keys being supplied that aren’t in the model?

Thanks for answer,
I mean the second one you told. In the huge data text file, in every line, there are different datas for example ( CIS=155| SIN=‘hello.com’ | … ). And on my model class, as well as the database, there is a table with these columns.

Incoming data is pretty reliable that I am sure it will be in the same structure with exact same keywords of data (like CIS and SIN in the example), cause of that I just need some preferably periodical function which inserts these data to the appropriate columns in my table

Ok, so what you will be looking to do is to process this file one line at a time.

For each line, you’ll want to retrieve the appropriate row from the model.

Then, for that row, you’ll want to update the fields. If the keys are expected to exactly match field names in the model, and that you don’t have any keys that aren’t fields, you can use the update function on the row passing the dict as the parameter to that function.
(See my comment at Reusable Queries with Q - #2 by KenWhitesell regarding the use of a dict as a parameter to a function.)