Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The formatting does not work on read-only fields #21

Open
tahmidkhan opened this issue Oct 21, 2018 · 10 comments
Open

The formatting does not work on read-only fields #21

tahmidkhan opened this issue Oct 21, 2018 · 10 comments

Comments

@tahmidkhan
Copy link

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?

@kevinmickey
Copy link
Owner

Try making the field disabled -- see #9. I'm not sure how this interacts with editable=False though -- let me know if it works for you.

@tahmidkhan
Copy link
Author

Try making the field disabled -- see #9. I'm not sure how this interacts with editable=False though -- let me know if it works for you.

Nope, does not seem to work.

@kevinmickey
Copy link
Owner

What versions of Python, Django, and django-prettyjson are you using?

What combination of disabled, editable, and readonly did you try?

@tahmidkhan
Copy link
Author

Python 3.3, Django 2.1, and latest for this library. Editable is set to False in my models.

@mjhanke
Copy link

mjhanke commented Jun 11, 2019

These lines of code in the django source could be helpful. It sets all fields to exclude, which will likely affect the callback later on, which is what uses the formfield_overrides

@BenVosper
Copy link

I can confirm that including a JSONField in readonly_fields prevents the PrettyJSONWidget from being used.

But setting the field disabled does have the desired effect: the PrettyJSONWidget is rendered but you can't edit the text-area.

This can be done by overriding ModelAdmin.get_form:

def get_form(self, *args, **kwargs):
    form = super().get_form(*args, **kwargs)
    my_json_field = form.base_fields["my_json_field"]
    my_json_field.disabled = True
    my_json_field.widget = PrettyJSONWidget(attrs={"initial": "parsed"})
    return form

Working with Django 2.2.5, django-prettyjson 0.4.1

@jordanmkoncz
Copy link

I ran into this issue too. I tried your workaround @BenVosper and while this works for a user who has elevated permissions for a model, if the user only has the "view" permission for a model then Django automatically makes all fields read-only and as such django-prettyjson does nothing and the user just sees the plain text / string version of the JSON.

@kevinmickey any thoughts on how to get django-prettyjson to work for read-only fields?

@raphodn
Copy link

raphodn commented Apr 29, 2021

Did any of you find a solution to easily format readonly JSONFields ?

@OmarGitHubDev
Copy link

Add this to your admin instead of making the field read only

formfield_overrides = {
        django.db.models.JSONField: {
            'widget': django_json_widget.widgets.JSONEditorWidget(options={
                'mode': 'view',
                'modes': ['view']
            })
        },
    }

@marcussilvait
Copy link

Since including a JSONField in readonly_fields prevents the PrettyJSONWidget from being used, instead of overriding ModelAdmin.get_formas suggested, it is possible to pass the disable parameter as an attribute for PrettyJSONWidget too:

formfield_overrides = {
    JSONField: {
        "widget": PrettyJSONWidget(
            attrs={"initial": "parsed", "disabled": True}
        )
    }
}

Working with Django 3.1.13, django-prettyjson 0.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants