adding new key-value pair to existing data through django ORM

64 views
Skip to first unread message

KAUSHAL SINGH

unread,
Oct 21, 2014, 3:40:51 AM10/21/14
to django...@googlegroups.com
Hello,

How can we add new key-value pair to the existing data, through django-orm?

Suppose, created a data with:
instance = Something.objects.create(name='something', data={'a': '1', 'b': '2'})

And now i want to add one more pair: 
{'c': '3'}

is there any method to do so? any help to do so?


Max Demars

unread,
Oct 21, 2014, 8:06:09 AM10/21/14
to django...@googlegroups.com
instance.data['c'] = 3

instance.save()

--
You received this message because you are subscribed to the Google Groups "django-hstore" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-hstor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Krzysztof Nazarewski

unread,
Nov 22, 2014, 9:04:27 AM11/22/14
to django...@googlegroups.com
Is it possible to do the same in a database query?
I need to update potentially hundreds of objects with a new field.

Krzysztof Nazarewski

unread,
Nov 22, 2014, 9:42:04 AM11/22/14
to django...@googlegroups.com
Ok i found the way, first i need to get handle of HStoreManager:
class Something(models.Model):
    data = hstore.DictionaryField()
    objects
= hstore.HStoreManager()
then we can do:
Something.objects.all().hupdate(attr='data',updates={'c':'3'})
Reply all
Reply to author
Forward
0 new messages