-
Notifications
You must be signed in to change notification settings - Fork 89
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
Widget doesn't appear to support "disabled" behavior #38
Comments
I assume you are not adding the desired fields to readonly_fields list/tuple. Because, as soon as you add, they are rendered as plain TextArea. If you are doing it this way, your admin form is open to a security threat. Though at the editor level, there is no option to edit JSON data but think of the user submitting a different payload using raw endpoints. It accepts and modifies! The best way IMHO is: Define a form for your payload
Then in your admin
The advantage of the above approach is two-fold, all JSONFields are not getting subjected to a similar setting. And, at the editor level, the user is presented with only the 'view' mode. Even if the user tampers and tries to submit a different payload, the changes are dropped in favor of existing data since we set disabled to True. |
Here is my solution: https://gist.github.com/elonzh/413df4532e491de27f9a51e9dbf7e8c1 Django admin render django/contrib/admin/templates/admin/includes/fieldset.html
JSONAdminMixinclass JSONAdminMixin:
formfield_overrides = {
models.JSONField: {"widget": JSONEditorWidget},
}
class ReadonlyJSONWidget(JSONEditorWidget):
template_name = 'django_json_readonly_widget.html'
@property
def media(self):
return super().media + self.ReadonlyJSONWidget().media
@classmethod
def render_readonly_json(cls, name, value, attrs=None, mode='code', options=None, width=None, height=None):
attrs = attrs or {}
attrs.setdefault("id", "id_" + name)
widget = cls.ReadonlyJSONWidget(attrs, mode, options, width, height)
return widget.render(name, value) django_json_readonly_widget.html
PayloadAdmin:class PayloadAdmin(JSONAdminMixin, admin.ModelAdmin):
fields = ["payload"]
def payload_display(self, obj):
return self.render_readonly_json("payload", json.dumps(obj.payload)) |
Description
The widget doesn't appear to support the "disabled" html attribute. When I set associated Django field (e.g., JsonField) to disabled, it doesn't render as disabled and still allows the user to make edits in the widget.
If there is a way to render the widget, but in a "disabled" state, I would like to know how to specify that on the widget.
Work around
Set options to restrict mode to read-only text mode and prevent the user from selecting other modes.
Downside: This affects all JSONFields on the form and may not want ALL to be read-only.
formfield_overrides = {
fields.JSONField: {'widget': JSONEditorWidget(options={"mode": "text", "modes": ["text"] })},
}
The text was updated successfully, but these errors were encountered: