jerick jerick - 1 year ago 48
JSON Question

JSONField custom error message

I'm currently reviewing an application i'm working on which use JSONField for json entries in the model like so.

json_config = JSONField(blank=True)

I noticed that if I'm editing the model in django admin and if the json is invalid, the field returns the following message

Enter valid JSON

which is good but not entirely helpful as i sometimes don't know exactly where the error is in the JSON. For example, if i use simplejson.loads() for strings which look like json objects, it returns a message indicating approximately where the line and character is where the error occurs.

Invalid control character u'\r' at: line 5 column 16 (char 78)

I couldn't find something similar to it for JSONField. Is there i way to do that?


Answer Source

It's a bit late but what i did for this was, on the form itself, i used a TextField whose input i then pass to simplejson.loads() which in turn returns the error message that I need back to the form. Once everything checks out with the json and is valud, i pass the contents of the simplejson.loads() to the JSONField for saving.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download