Note that there are some explanatory texts on larger screens.

plurals
  1. POUse forms.TextArea for custom JSON field in Django admin site
    text
    copied!<p>I'm using a custom Django field to represent JSON encoded data:</p> <pre><code>class JSONField(models.TextField): __metaclass__ = models.SubfieldBase def to_python(self, value): if value == "": return None try: if isinstance(value, basestring): return json_decode(value) except ValueError: pass return value def get_prep_value(self, value): if value == "": return None if isinstance(value, dict) or isinstance(value, dict): value = json_encode(value) return super(JSONField, self).get_prep_value(value) def value_to_string(self, obj): value = self._get_val_from_obj(obj) return self.get_db_prep_value(value,connection=None) </code></pre> <p>The field itself works fine. However editting via the admin site is not possible since the string from the database is JSON-decoded and converted to a dictionary, so when the admin site is rendered, not the actual JSON string from the database is displayed (e.g. {"foo": "bar"}), but its dictionary representation (e.g. {u'foo': u'bar'}).</p> <p>Obviously this leads to problems when saving the database object, because the dictionary's string representation is not a valid JSON string.</p> <p>What I'd like to have is the admin site showing the actual database value (i.e. the string as it is saved in the database), instead of the string representation of the Python object returned by to_python.</p> <p>My attempt would be to write a custom widget for this that just calls json_encode again on the dictionary - but is there a better way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload