views:

28

answers:

2

I have a JSONField (http://djangosnippets.org/snippets/1478/) in a model and I'm trying to find out the best way to display the data to the admin user rather than json.

Does anyone know the best way to do this in django admin?

For instance, I would like

{u'field_user_name': u'foo bar', u'field_email': u'[email protected]'}

to display as

field_user_name = foo bar
field_email = [email protected]
A: 

Maybe you should create a template filter to do this :

from django import template
from django.utils import simplejson as json

register = template.Library()

@register.filter
def json_list(value):
    """
    Returns the json list formatted for display
    Use it like this :

    {{ myjsonlist|json_list }}
    """
    try:
        dict = json.loads(value)
        result = []
        for k, v in dict.iteritems():
            result.append(" = ".join([k,v]))
        return "<br/>".join(result)
    except:
        return value
Ghislain Leveque
This is great, but is there any way to do this without using templating? I would like to be able to use this in many different apps, but this approach could get cumbersome.
kayluhb
+1  A: 

Perhaps create a custom widget?

class FlattenJsonWidget(TextInput):
    def render(self, name, value, attrs=None):
        if not value is None:
            parsed_val = ''
            for k, v in dict(value):
                parsed_val += " = ".join([k, v])
            value = parsed_val
        return super(FlattenJsonWidget, self).render(name, value, attrs)
Brandon Konkle