abogushov / django-admin-json-editor

Adds json-editor for JSONField in Django Administration
MIT License
173 stars 61 forks source link

Populate enum values #6

Closed AdrienLemaire closed 6 years ago

AdrienLemaire commented 6 years ago

Hi, I'm having another issue regarding populating an enum field from db values.

Here's an example:

class HostAdminForm(forms.ModelForm):
    class Meta:
        model = Host
        fields = '__all__'
        HOST_ROLES_SCHEMA['items']['properties']['categoryId'] = {
            'title': 'category',
            'type': 'string',
            'enum': [1, 2, 3, 4, 5],   #  ← We want to get the list of category ids from the db here
        }
        widgets = {
            'details': JSONEditorWidget(HOST_DETAILS_SCHEMA, collapsed=True),
        }

Any idea how we could proceed to do this ? Until now, we've asked our users to figure out what the id was and paste it in a text input :/

abogushov commented 6 years ago

@Fandekasp Hi, you can pass a function as argument of JSONEditorWidget and generate schema for widget dynamically. I will add an example of this.

abogushov commented 6 years ago

@Fandekasp I have added an example of dynamic schema.

AdrienLemaire commented 6 years ago

Thank you so much @abogushov , I really appreciate