bhch / django-jsonform

A better, user-friendly JSON editing form field for Django admin. Also supports Postgres ArrayField.
https://django-jsonform.rtfd.io
BSD 3-Clause "New" or "Revised" License
307 stars 31 forks source link

Define defaults for an array #158

Open captain828 opened 4 months ago

captain828 commented 4 months ago

Is there a way to define defaults for an array?

{
    'type': 'array',
    'items': {
        'type': 'string',
        'choices': ['Eggs', 'Milk', 'Juice'],
        'widget': 'multiselect'
    }
}

I've tried:

{
    'type': 'array',
    'items': {
        'type': 'string',
        'choices': ['Eggs', 'Milk', 'Juice'],
        'widget': 'multiselect'
    },
   'default': ['Eggs', 'Milk']
}

but it's not working.

bhch commented 4 months ago

I'm unable to reproduce this. It's working fine in the playground.

You can also test your schema in the playground (be sure to make it valid json i.e. use double quotes instead of single).

Also, which version are you using. Find out using this command:

$ python -c "import django_jsonform; print(django_jsonform.__version__)"
captain828 commented 4 months ago

Hhhm, it does work on the playground and also if I give it a schema on the model field but not via Javascript, using

form.update({
            schema: schema,
            data: form.getData()
        });

The actual schema I am sending is:

{
    "default": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"],
    "items": {
        "enum": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
        "type": "string",
        "widget": "multiselect"
    },
    "type": "array"
}

(generated from a Pydantic model)

All other defaults are working fine.

bhch commented 4 months ago

This might be a bug or it might be the expected behaviour.

I don't think the form will populate the defaults on updates.

E.g. let's say a user has de-selected the defaults from the input, and so later on when update() is called, the form should keep the array empty instead of re-populating the defaults because the user had already edited the input and removed them.

Though I'm not exactly sure if this is what's really going on.

If you could provide me exact code to reproduce this, I will be conclusively able to tell whether it is a bug or not.