django-unfold icon indicating copy to clipboard operation
django-unfold copied to clipboard

Add support for django-jsonform

Open Mte90 opened this issue 1 year ago • 8 comments

ref: https://github.com/bhch/django-jsonform

I added it in my app but I can't see the JS stuff so probably is missing some integration specific to load the different HTML and JS for this library.

Mte90 avatar Nov 10 '23 14:11 Mte90

So I was able to integrate it but is a workaround:

    ITEMS_SCHEMA = {
        'type': 'array',
        'items': {
            'type': 'integer'
        }
    }
    formfield_overrides = {
        JSONField: {
            "widget": JSONFormWidget(schema=ITEMS_SCHEMA),
        }
    }

As you can see the parameter schema is custom one and overriding let's to load the right field but it is missing that parameter. With this workaround I am placing twice the schema in the admin and the model but it should get it from the model.

Mte90 avatar Nov 10 '23 15:11 Mte90

I tried patching the widgets.py to add the new unfold field with:


try:
    from django_jsonform.widgets import JSONFormWidget
    class UnfoldAdminJSONFormWidget(JSONFormWidget):
        def __init__(self, attrs=None, schema=()):
            super().__init__(attrs, schema)

except ImportError:

    class UnfoldAdminJSONFormWidget:
        def __init__(self, *args, **kwargs):
            raise UnfoldException("django-jsonform not installed")

And in admin.py:


try:
    from django_jsonform.widgets import JSONFormWidget
    HAS_JSONFORM = True
except ImportError:
    HAS_JSONFORM = False


if HAS_JSONFORM:
    FORMFIELD_OVERRIDES.update(
        {
            models.JSONField: {"widget": UnfoldAdminJSONFormWidget},
        }
    )

But I am not able to get the schema parameter passed in the model.

Mte90 avatar Nov 10 '23 15:11 Mte90

Massive thumbs up for this - @lukasvinclav if you're keen on this, then in the new year when I'm back in office I could discuss partly sponsoring the feature, as it would be mega useful for me to connect to postgres' JSONField.

@Mte90 perhaps your organisation could co-sponsor the work, if it's for something that ends up client-facing?

It'll make a nice pairing with the work on django-svelte-jsoneditor - which you might also be interested in, @Mte90, if you're doing jsony things with django!

thclark avatar Dec 21 '23 10:12 thclark

At the end I switched to https://pypi.org/project/django-select2/ as it wasn't working great also on frontend (I am in a prototype phase right now) and the content value wasn't working with that package (I tried to get jsonform on backend and select2 on frontend).

The integration was more nicer also if I have an issue with the JS library used https://github.com/select2/select2/issues/6287

Mte90 avatar Dec 21 '23 11:12 Mte90

Ah right, OK. I'm going to give it a try now, I've been searching for a jsonschema form integration for a long while now. There are a few offerings out there but I hadn't come across this, and it looks much more comprehensive and better maintained than the others. I'll report back.

thclark avatar Dec 21 '23 11:12 thclark

After a couple of hours of playing I'd say this is a bit of a struggle in some ways but can be achieved overall. It's reasonably well designed fin terms of the architectural choices made.

The styling looks horrible on top of unfold, but it is just about usable. So I think my verdict is I'm going to try it out a bit more and if I start using it in a bit more anger on customers projects I'll come back here and give this a poke that I'm willing to sponsor.

Pic attached for prrof of concept that it's possible to render the widget into unfold.

Screenshot 2023-12-21 at 13 42 42

thclark avatar Dec 21 '23 13:12 thclark

the screenshot is missing :-)

Mte90 avatar Dec 21 '23 14:12 Mte90

the screenshot is missing :-)

fixed!

thclark avatar Dec 21 '23 14:12 thclark

For third party application integration you can contact us here https://unfoldadmin.com/

lukasvinclav avatar Jul 05 '24 10:07 lukasvinclav