Append to models.JSONField on POST

610 views
Skip to first unread message

chris jess

unread,
Feb 16, 2017, 11:11:50 AM2/16/17
to Django users
Can anyone let me know the best way to append to a models.JSONField on HTTP POST?

I would prefer to simply append to the existing JSONField as opposed to updating the values in it.

Is it possible to do this without first loading that object?

Thanks.
Message has been deleted

ludovic coues

unread,
Feb 16, 2017, 11:24:06 AM2/16/17
to django...@googlegroups.com
You can't.

There is 7 value types in JSON.
3 of them are constant, true, false and null. Appending to them make no sense.
You could append to number, but then you change its value.
String are double quote delimited. You can't append to it, you need to
insert before the closing double quote. aka updating.
Any way, I'm pretty sure JSONField don't let you use these values as
root object for some reason.

The two remaining value types are array and object. Both suffer from
the same issue as string, they are delimited and you can't simply
append at the end, you need to insert data, aka updating.


If you know what you are doing, read the source, look at how to update
the raw value, write some test to make sure what you are doing is
working now and is working each time you upgrade django.

I wish you good luck if you go down that path and to have a good experience.
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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/2c044960-3a7a-4997-ba43-eeb7b1747566%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

chris jess

unread,
Feb 16, 2017, 11:30:56 AM2/16/17
to Django users
Thanks. 

Was hoping there was a method for appending to a JSON array stored in a JSONField without fetching the whole record first.

ludovic coues

unread,
Feb 16, 2017, 11:38:38 AM2/16/17
to django...@googlegroups.com
If there is one, I've never heard of it. But I'm not an heavy user of
postgres or its JSONField.
Maybe someone else know better than me :)
> https://groups.google.com/d/msgid/django-users/f39ffbe6-e1e2-457f-9298-932ee2574306%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages