Monday, March 27, 2017

Re: Update dict JsonField Postgresql

Perfect!! This code works:

data = objectitem.data

data[0]['processed'] = True

objectitem.data = data

objectitem.save()


A last query that is not related to the insertion, but to the query.

If I want to get only records processed in True as I have to do?

This does not work, I get all the records:

objectitem.filter(data__contains=[{'processed': True}])

Thanks!!!



El lunes, 27 de marzo de 2017, 14:29:13 (UTC-3), Melvyn Sopacua escribió:

Hi,

 

On Monday 27 March 2017 08:50:04 Martin Peveri wrote:

 

Totally forgot about this gotcha:

 

> > > >>> objectitem.data[0]['processed'] = True

 

That won't work.

objectitem.data is a reference to a list of dicts

changing something inside, doesn't signal the model that it's data has changed, as the reference doesn't change.

 

You need to manipulate the data as it's own structure, then assign it:

 

data = objectitem.data

data[0]['processed'] = True

objectitem.data = data

objectitem.save()

 

If the above don't work:

objecitem.save(force_update=True, update_fields=('data',))

 

 

--

Melvyn Sopacua

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6951c970-5ee8-496e-a965-73da5ec3600f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment