kevinmickey / django-prettyjson Goto Github PK
View Code? Open in Web Editor NEWEnables pretty JSON viewer in Django forms, admin, or templates
License: BSD 3-Clause "New" or "Revised" License
Enables pretty JSON viewer in Django forms, admin, or templates
License: BSD 3-Clause "New" or "Revised" License
If I have ["HH"] stored in a jsonfield, when I edit the corresponding model, using django-prettyjson shows it with backslashes in attached screenshot
When user saves the same string again, the backslashes become part of the json data. This means everytime a user clicks on change form to edit some other field, the json field will continue to get backslashes added.
This is not a good experience. Ideally, there should be a conversion to show string without backslashes to the user
Can the strings be translatable please?
I was hoping I could at least replace a template file but the button strings are hard-coded in the js file.
It would be better if this could be translatable in a way or another…
My code setup is as follows:
models.py
class MyModel(models.Model):
structure = JSONField(default=dict, blank=True, null=True)
static_structure = JSONField(default=dict, blank=True, null=True, editable=False)
admin.py
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
JSONField: {'widget': PrettyJSONWidget }
}
def get_readonly_fields(self, request, obj=None):
if obj:
return ('static_structure')
else:
return super(MyModelAdmin, self).get_readonly_fields(request, obj)
admin.site.register(MyModel, MyModelAdmin)
In other words, one of the JSONFields is not editable and read-only. It appears on my admin panel, but its not being affected by the widget which renders the other field as formatted JSON.
Is this by design? And is there any way to have the formatting work for read-only fields?
See https://github.com/audreyr/standardjson
Fully tested, compliant with the official ECMA-262 and ECMA-404 JSON specifications
Using 0.2.1 on Django 1.10.5, when I enable the debug toolbar, the JSON widgets print text from DJANGO settings. It does not happen for every widget, but I have pages with many JSON Widgets and it happens on every page load.
Disabling the toolbar and everything goes back to normal.
I have not investigated further but thought this may save someone some time.
would it not be possible to use DjangoJSONEncoder
instead of standardjson
?
class DjangoJSONEncoder(json.JSONEncoder):
"""
JSONEncoder subclass that knows how to encode date/time, decimal types, and
UUIDs.
"""
Thanks for this very nice and useful widget! I'm pretty certain, by looking at the doc and code, that there is no option to start the widget not only "parsed", but also "collapsed": if that's the case, and that my assumption that it would be fairly easy to do is correct, would you accept a PR for doing so?
I'm using django-prettyjson with Django 2.0 and there seems to be a problem with the order in which the scripts are running. The prettyjson.js file is getting run before the Django jQuery gets loaded and so it throws an error. I was able to fix it with the following snippet:
class FixedPrettyJSONWidget(PrettyJSONWidget):
class Media:
js = ('admin/js/jquery.init.js', 'prettyjson/prettyjson.js', )
css = {
'all': ('prettyjson/prettyjson.css', )
}
I'm not sure if there is a better way to do it, but hopefully this illustrates the problem.
Using 0.2.2 on Django 1.9.4
It seems to not work with readonly_fields.
I think PrettyJSONWidget's render may run before render readonly_fields.
maybe initial=raw (default) and initial=parsed
I can't seem to figure out how to do
`
{% load prettyjson %}
{% prettyjson_setup %}
{% prettyjson mydict %}
` and just show the json already pretty-fied/rendered ?Help pls!
Module six should be included in the requirements as it is required in 'prettyjson.templatetags.prettyjson'
Had ImportError when adding 'prettyjson' to installed apps on django 1.10.5 and python 3.5.2
Hello, I noticed that when you are returning a custom error in a ModelForm that uses the PrettyJsonWidget(), there is an indication that there is an error in that field, but it does not show the error message. In my use case I am overriding the clean() method on the ModelForm and using the add_error() method to return a custom error message on the field. When I use a TextField instead of the PrettyJsonWidget the error shows up, but does not when I am using the supplied widget.
I've been integrating with django-prettyjson and in some of my fields the ability to auto sort by key alphabetically in the parsed ("pretty") view would make it even prettier.
Thanks for the great library :)
When loading the widget as initially parsed in the admin, it takes the width and height of the original textarea, making it often too small. The parsed view takes considerably more space, and will often result in unnecessary scrolling:
This is easily fixable in the js, and I've got a fix ready. But I was wondering if you were set on the current behavior, of it there was a specific reason for how it currently works.
Wondering if it's possible to incorporate this "in row" within a table. As an example if you have a column within your table that is json, can we include an inline widget for each row & json cell to format.
standardjson should be required when it is installed.
Invalid template library specified. ImportError raised when trying to load 'prettyjson.templatetags.prettyjson': No module named standardjson
As we can see in the release notes for Django 2.1 (https://docs.djangoproject.com/en/2.1/releases/2.1/):
Support for Widget.render() methods without the renderer argument is removed.
As this project does not have a renderer
kwarg, it fails when rendering:
def render(self, name, value, attrs=None):
It should be enough to just add:
def render(self, name, value, attrs=None, **kwargs):
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.