开发者

display json text as a friendly list in django admin

I have a JSONField (http://djangos开发者_开发技巧nippets.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'foo@bar.com'}

to display as

field_user_name = foo bar
field_email = foo@bar.com


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)


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜