JSONField: which app to choose ?

456 views
Skip to first unread message

Michael Palumbo

unread,
Jul 9, 2012, 2:52:33 PM7/9/12
to django...@googlegroups.com

Reinout van Rees

unread,
Jul 10, 2012, 4:08:50 PM7/10/12
to django...@googlegroups.com
On 09-07-12 20:52, Michael Palumbo wrote:
> Hi,
>
> I have found several implementations of a Django JSON Field.
> Have you ever tried one ? Which one do you recommend ?

I'm using the 'django-jsonfield' on pypi:
http://pypi.python.org/pypi/django-jsonfield/

It is https://bitbucket.org/schinckel/django-jsonfield/ on bitbucket.
I looked at it with a colleague and it seemed to have a bit more
validation than some others. Little things.


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"



Michael Palumbo

unread,
Jul 10, 2012, 5:51:45 PM7/10/12
to django...@googlegroups.com
Can you get it work in the admin ?
I don't understand why I get a ValidationError  "[u"'' is not a valid JSON string."] as soon as I try to reach the Add admin page of my Model...

import jsonfield
class Mapping(models.Model):
    data_map4 = jsonfield.JSONField()
-----
class MappingAdmin(admin.ModelAdmin):
    fields = ['data_map4']
    list_display = ('data_map4',)

Thanks.

Matt Schinckel

unread,
Jul 11, 2012, 2:22:57 AM7/11/12
to django...@googlegroups.com
Hi Michael.

It should 'just work' in the admin: I'm using it fairly extensively, and most of our access is through the admin (or a JSON api).

If we can get a minimal failing test, then I'll make sure it gets fixed.

(And thanks, Reinout, for the referral/comment)

Matt.

Matt Schinckel

unread,
Jul 11, 2012, 3:15:23 AM7/11/12
to django...@googlegroups.com
Michael,

I've been able to reproduce it: I'm not quite sure of the best way to deal with it.

One solution is to set either null=True, or blank=True on the field, or set a default.

The trick is, an empty string isn't valid JSON. I guess I've always been using a default (usually of {} or [], depending upon context).

Happy for you to suggest improvements, though.


Matt.

Michael Palumbo

unread,
Jul 11, 2012, 5:28:49 PM7/11/12
to django...@googlegroups.com
Hi Matt,

Correct, using either null=True or blank=True on the field works.
However, I don't get it since I didn't click on "save", I just wanted to display the add page containing it. Why does it attempt to validate the field at this time and not wait for the form to be sent ? Maybe this is how Django works ?

Whatever, I think you can set a default value in the JSONField to avoid the error, what do you think ? I think it's handy.
improvement:
    def __init__(self, *args, **kwargs):
        kwargs['default'] = kwargs.get('default', '{}')
        super(JSONField, self).__init__(*args, **kwargs)

It works for me.

Another point: it does not like "extra" comma right ?
{
  "nodes": { "title": "title", }
}
But I guess this is wanted because JSON does not allow it ?


Michael
Reply all
Reply to author
Forward
0 new messages