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

Add support for django-jsonform #190

Open
Mte90 opened this issue Nov 10, 2023 · 8 comments
Open

Add support for django-jsonform #190

Mte90 opened this issue Nov 10, 2023 · 8 comments

Comments

@Mte90
Copy link

Mte90 commented Nov 10, 2023

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
Copy link
Author

Mte90 commented Nov 10, 2023

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
Copy link
Author

Mte90 commented Nov 10, 2023

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.

@thclark
Copy link

thclark commented Dec 21, 2023

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!

@Mte90
Copy link
Author

Mte90 commented Dec 21, 2023

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 select2/select2#6287

@thclark
Copy link

thclark commented Dec 21, 2023

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
Copy link

thclark commented Dec 21, 2023

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

@Mte90
Copy link
Author

Mte90 commented Dec 21, 2023

the screenshot is missing :-)

@thclark
Copy link

thclark commented Dec 21, 2023

the screenshot is missing :-)

fixed!

@lukasvinclav lukasvinclav changed the title Integration with django-jsonform Add django-jsonform support Dec 21, 2023
@lukasvinclav lukasvinclav added the enhancement New feature or request label Dec 21, 2023
@lukasvinclav lukasvinclav changed the title Add django-jsonform support Add support for django-jsonform Jan 6, 2024
@lukasvinclav lukasvinclav added third party and removed enhancement New feature or request labels Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants