django-jsonform
django-jsonform copied to clipboard
Add allOf/anyOf/oneOf concept
One of the very useful features in JSON Schemas is the option to have multiple options via allOf/anyOf/oneOf
for the same key/set of keys when nesting. For our project github.com/cividi/spatial-data-package-platform this would solve a major hurdle for fully embracing django-jsonform as an elegant solution for tenant defined forms. Would be awesome to know if this is at all thinkable as a future addition. Thanks so much for your efforts.
Yeah, currently I've implemented a custom, non-standard json schema specification. But I'm working towards adding the standard features. I've got some features lined up for the next release (like referencing and recursive nesting). I'll try to include allOf/anyOf/oneOf
also. I'll have to see.
Currently, for basic string based options, you can use the choices
keyword. And for anyOf
type options, there's a multiselect
widget.
I'm starting work on this. But these keywords - allOf/anyOf/oneOf
- are mainly for validation and I'm having a hard time trying to figure out how to render a form for them.
If you can give me a couple of example schemas for them and describe the form's expected behaviour, it will make things a bit easier for me. Thanks.
@n0rdlicht Hi, I'm still waiting for suggestions from you. Please, see my previous comment. Thank you.
oneOf is useful as it allows us to map Django TextChoices cleanly to a select field. Otherwise one must use the pre_save_hook to transform the choice label to the choice value.
@danihodovic I will try to add these features in the next release. These days I'm caught up in some other things, but hopefully by mid to late February.
@bhch Many thanks for this excellent library. It's one of the best tools I've found for unstructured data (with a user interface). I'm using it for field translations, but I can think of a dozen other use-cases for early stage prototyping. This project deserves more attention.
A great use case for this would be to build forms for variable field sets for inheritance-simiar discriminated unions, as they are for instance implemented by Pydantic. https://docs.pydantic.dev/usage/types/#discriminated-unions-aka-tagged-unions
This is a very common JSON/JSON Schema pattern.
@gersmann I'm working on this. I've got about half of this done. I'll try to get a beta out this weekend for some feedback.
Hello, everyone participating in this thread!
I've just implemented this feature on the JS side. You can test it out in the playground here: https://bhch.github.io/react-json-form/playground/#18-anyof.
I encourage you to kindly test with your own schemas because there might be certain cases which I've forgot to account for. If you do find any bugs, please report. But any sort of feedback is appreciated.
I'll release the Django app next weekend.
Thank you.
Worked like a charm on my test schema. Very nice, thats a game changer for working with Pydantic in Django.
This feature has been implemented across multiple commits:
- a24ba9a413fad79de66c0b83440f55fa8ceb3371 (js, initial implementation)
- 81b56c0331d479fc7cabc904e2698ece2ca5020b (css)
- a9ddc619ccfcac78148515cb3977caa1d58bac0b (js bugfixes)
- 92ef8e9128327709351a62e20c8e52268271fb0e (css)
- acc84d3cef1be97abec7b6f6d86ef14414de46d6 (validation and tests)
- 9f0b284d35c76e50d13f06da8ba0d8d0b717c15a (js bugfixes)
Will be released shortly.
This feature has been released in v2.16.0. Please update and test it out.
There are a few limitations in the implementation, so do read the docs as well.
Thank you.