Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save django object using dictionary?

is there a way that I can save the model by using dictionary

for e.g. this is working fine,

p1 = Poll.objects.get(pk=1)

p1.name = 'poll2'
p1.descirption = 'poll2 description'

p1.save()

but what if I have dictionary like { 'name': 'poll2', 'description: 'poll2 description' }

is there a simple way to save the such dictionary direct to Poll

like image 901
shahjapan Avatar asked Apr 23 '10 07:04

shahjapan


1 Answers

drmegahertz's solution works if you're creating a new object from scratch. In your example, though, you seem to want to update an existing object. You do this by accessing the __dict__ attribute that every Python object has:

p1.__dict__.update(mydatadict)
p1.save()
like image 111
Daniel Roseman Avatar answered Oct 01 '22 20:10

Daniel Roseman