6

I have a django app, using also rest_framework, and a model Product with field of type JSONField. so data is stored as JSON in Postgres, Now I want to provide the admin with a nice user friendly way on how he can change the json field (names/keys and values). is there an extension for that or is there a faster way on how to do that.

here is the column definition in the database. my_column = JSONField(default={"editorial1": "text 1", "editorial_2": "text2", "editorial_3": "text"})

BOTH KEYS AND VALUES SHOULD BE EDITABLE BY THE ADMIN

The admin should not know anything about JSON, and should not enter/edit any json format field

anyavacy
  • 1,618
  • 5
  • 21
  • 43

3 Answers3

5

You can use prettyjson's PrettyJSONWidget:

class ProductModelForm(forms.ModelForm):
    class Meta:
        fields = (
            ...
            'my_column',
        )
        widgets = {
            'my_column': PrettyJSONWidget(),
        }
Botte Oleksandr
  • 159
  • 1
  • 9
  • 1
    `pip install django-prettyjson` (I use 0.3.0, but there can be a newer one out there) – Botte Oleksandr Aug 01 '18 at 14:05
  • thnks for the info. this would require that the use have to have some knowledge about json. our users do not know anything about that. therefore the https://github.com/kevinmickey/django-prettyjson seemed to be better i would say – anyavacy Aug 01 '18 at 14:56
3

I ended up using the django-admin-json-editor. Not the best thing in the world, but it does the trick

https://github.com/abogushov/django-admin-json-editor

anyavacy
  • 1,618
  • 5
  • 21
  • 43
2

You can try https://github.com/jrief/django-entangled

In comparison to the editors mentioned above, it doesn't replace the widget used to render the JSON, but allows to override the ModelForm which otherwise is generated by Django's ModelAdmin.

jrief
  • 1,516
  • 13
  • 9