jmrivas86 / django-json-widget

An alternative widget that makes it easy to edit the new Django's field JSONField (PostgreSQL specific model fields)
MIT License
440 stars 88 forks source link

Allow field validation using JSON schema #66

Open jayvdb opened 2 years ago

jayvdb commented 2 years ago

Description

I would like to supply a JSON-schema to the widget, and the widget ensures that the field content is validate according to the JSON schema provided.

KARTHIKEYAN-ACKO commented 2 years ago

You can give that via the options

JSONEditorWidget(
                options={

                    "schema": self.acl_schema,
                }
            )
Gustutu commented 1 year ago

i used it with dynamic json schema stored in a related model. Id dit it by overriding get_form method :

   def get_form(self, request, obj=None, change: bool = False, **kwargs):
        my_form = super().get_form(request, obj, change, **kwargs)
        categorie_spec = Categorie.objects.get(
            id=obj.categorie_id
        ).specifications_schema
        my_form.base_fields["specifications"].widget = JSONEditorWidget(
            options={"schema": categorie_spec}
        )
        return my_form