Skip to content

Enhancement: JSONField should allow for pretty printing #681

@fh-igor-serko

Description

@fh-igor-serko

Is your feature request related to a problem? Please describe.
When using the JSONField I would want to see the JSON content pretty printed.

Describe the solution you'd like
Currently starlette-admin uses the json.html template for rendering JSONField.
It uses the tojson filter, initial assumption was that it's the Jinja2's version, but it is not and starlette-admin seems to override it.

So I think the solution here is to just remove the override and rely on Jinja2's internal version. Not sure whether default=str is something we need to worry about though.

The solution for pretty printing would then be to create a separate template file in your application, like:

# templates/admin/json_pretty_print.html
<div id="{{ field.name }}" class="field-json">{{ data|tojson(indent=2) }}</div>

And then use it on a JSONField, like:

class MyModelView(ModelView):
    fields = [
        JSONField("json_data", display_template="admin/json_pretty_print.html")
    ]

Describe alternatives you've considered

  • Could just add the indent option to the lambda function. However looking closely at the Jinja implementation, it does run htmlsafe over it, while the starlette-admin lambda doesn't.
  • I found no way to override which filters get loaded into starlette-admin

Additional context
n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions