سؤال

How can I add data to a dictionary in mongoengine?
I do not have the data to save the entire dict again, I only have one item I want to add at a time.

I have tried using:

Lookups.objects(pk="52d3a8e318fbaf0e1075de4f").update(push__schools=new_schools)

But I think the reason why this is not working is because push is to add an item to a list, not a dict. Can anyone please help me?

هل كانت مفيدة؟

المحلول

You should use $set eg:

Lookups.objects(pk="52d3a8e318fbaf0e1075de4f").update(set__schools__KEY=VALUE)

Updated:

If you want to set multiple keys then you can eg:

Lookups.objects(pk=x).update(set__schools__KEY=VALUE, set__schools__KEY1=VALUE1)

Taking a dict of keys and values - you can convert to a new dict and update like so:

new_schools = {"key1": "value1", "key2": "value2"}
set_new_schools = dict((("set__schools_%s" % k, v) for k,v in new_schools.iteritems()))
Lookups.objects(pk=x).update(**set_new_schools)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top