DRF and JSONField - Newbie

2,067 views
Skip to first unread message

ste...@activitystream.com

unread,
Mar 6, 2013, 12:53:47 PM3/6/13
to django-res...@googlegroups.com
Hi,

What am I doing wrong if a jsonfield:
  • properties = JSONField(_('Properties'), null=True, blank=True)
is being represented like this in the browsable API:
  • {u'key': u'value'}
instead of like this (I think is correct):
  • {"key": "value"}
This also prevents the value to be parsed/saved until corrected in the form. Meaning that the API will not accept values rendered by the same Browsable-API

Regards,
 -Stefan

ste...@activitystream.com

unread,
Mar 6, 2013, 12:59:29 PM3/6/13
to django-res...@googlegroups.com
Hi,

I should add that it's being represented like this in the form, not in the json serialized section above the form.

Regards,
  -Stefan

Scott White

unread,
Mar 6, 2013, 2:38:46 PM3/6/13
to django-res...@googlegroups.com
Hi Stefan,

I think what you are seeing in the first case is a Python dictionary with unicode strings. The second is JSON representation of that dictionary. You could see this yourself in a simple script:

import json
d = {'key':u'value'}
print d
print json.dumps(d, indent=4)

I may be wrong, but I don't believe JSONField is an available model field by default in Django. Are you creating your own JSONField? It may be the JSONField is actually storing a dict object?

Regards,
Scott

ste...@activitystream.com

unread,
Mar 6, 2013, 3:58:14 PM3/6/13
to django-res...@googlegroups.com
That's possible.

This is a round-trip issue as well.

Meaning that the form, or more the form values, rendered by DRF are not accepted by it if submitted.

Any ideas on how to fix that?

Regards,
 -Stefan

ste...@activitystream.com

unread,
Mar 7, 2013, 1:50:08 AM3/7/13
to django-res...@googlegroups.com
Hi,

I'm using the django-jsonfield which I have seen mentioned here and seen issues/fixes here regarding it's use/for presentation.

I assumed that it is supported (compatible).

Regards,
 -Stefan

On Wednesday, March 6, 2013 7:38:46 PM UTC, Scott White wrote:

Carlton Gibson

unread,
Mar 7, 2013, 3:17:19 AM3/7/13
to django-res...@googlegroups.com

On 6 Mar 2013, at 21:58, ste...@activitystream.com wrote:

Meaning that the form, or more the form values, rendered by DRF are not accepted by it if submitted.

You should be able to get this to work by creating a custom serializer field. 


You need to make sure to_native returns what the JSONField model field is expecting. 

Brief as it is, hopefully that helps.

Regards,

Carlton

ste...@activitystream.com

unread,
Mar 7, 2013, 10:46:20 AM3/7/13
to django-res...@googlegroups.com
Hi,

*fixing* this by changing the field_from_native and the to_native methods corrects the way it's stored.
At the same time it messes up the presentation by reversing the effect and escaping everything with "\" (JSON style).

Can anyone point me to a solution for DRF to deal with unstructured JSON content ?

Regards,
 -Stefan

Carlton Gibson

unread,
Mar 7, 2013, 11:14:19 AM3/7/13
to django-res...@googlegroups.com
Can you post your implementation. It sounds like you're essentially there.

C.

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

Andy Freeland

unread,
Apr 23, 2013, 12:30:53 PM4/23/13
to django-res...@googlegroups.com
Not the OP, but I'm working on the same thing: https://gist.github.com/rouge8/5445149

The issue is that if I provide the `to_native` method like that, my field ends up like this:

"data": "{\"domain\": \"andyfreeland.com\"}", 
But if I leave out the `to_native` method, the data field in the browsable API displays invalid JSON:

{u'domain': u'andyfreeland.com'}

Carlton Gibson

unread,
Apr 24, 2013, 8:36:15 AM4/24/13
to django-res...@googlegroups.com
Hi Andy, 

On 23 Apr 2013, at 18:30, Andy Freeland <an...@andyfreeland.net> wrote:

Not the OP, but I'm working on the same thing: https://gist.github.com/rouge8/5445149

The issue is that if I provide the `to_native` method like that, my field ends up like this:

"data": "{\"domain\": \"andyfreeland.com\"}", 
Your to_native is returning a string — which is then being escaped by the JSON render.

What obj is being passed to to_native? I'd guess you'd just want to return a dictionary here and all should work. 

But if I leave out the `to_native` method, the data field in the browsable API displays invalid JSON:

{u'domain': u'andyfreeland.com'}

I'm not sure what would cause this. It looks like a Python repr but the JSON renderer doesn't output those (does it?)

Regards,

Carlton


Andy Freeland

unread,
Apr 24, 2013, 8:53:15 AM4/24/13
to django-res...@googlegroups.com
It looks like the editable field uses the Python string representation and not JSON.

For example, when I provide `to_native`, it displays as the escaped JSON string in the API output, but in the edit form, it displays as {"domain": "andyfreeland.com"}. But when I don't provide `to_native`, it displays as correct JSON in the API output, but in the edit form, it shows up as the Python representation.

--
You received this message because you are subscribed to a topic in the Google Groups "django-rest-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-rest-framework/n0sLMpW4TqU/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to django-rest-fram...@googlegroups.com.

Andy Freeland

unread,
Apr 25, 2013, 5:25:08 PM4/25/13
to django-res...@googlegroups.com
For the moment, I've punted on a JSON field, and settled for a DictField: https://gist.github.com/rouge8/5463281

It still displays like 
{u'domain': u'andyfreeland.com'}
in the browsable API, but at least this way I can edit objects through the browsable API.
To unsubscribe from this group and all its topics, send an email to django-rest-framework+unsub...@googlegroups.com.

Rodrigo Gadea

unread,
Jan 8, 2014, 4:17:08 PM1/8/14
to django-res...@googlegroups.com
I have just dealt with this, and the easiest way I found is using the django-jsonfield package, which integrates smoothly with drf.

For the future: If you need this field and end up in this thread, go with that package, you will end up having some kind of NoSQL hybrid in SQL
Reply all
Reply to author
Forward
0 new messages