jrief / django-formset

The missing widgets and form manipulation library for Django
https://django-formset.fly.dev/
MIT License
317 stars 30 forks source link

Selectize with 'dynamic' choices, without model #144

Closed tobhv closed 3 months ago

tobhv commented 3 months ago

Hello,

selectize looks good. documentation mentions how to create a dynamic choicefield: https://django-formset.fly.dev/selectize/#usage-with-dynamic-number-of-choices How about nonmodel based dynamic choices ?

something like:


    group = DynamicChoiceField(
        label='groups of my third party system',
        widget=Selectize,
        choices=list_groups(),
        required=False
    )```

or are there any other options to reload the static ChoiceField somehow ?
(the choicefield already works with the list_groups() method. it;s just not dynamic / run again when i revisit the form unless the app gets restarted.

thanks!
tobhv commented 3 months ago

hm. answer to my own question:

    def __init__(self, *args, **kwargs):
        super(MyUserForm, self).__init__(*args, **kwargs)
        self.fields['group'].choices = list_groups()

issue solved..

jrief commented 3 months ago

A somehow cleaner way to solve this, is to create your own version of IncompleteSelectResponseMixin. Here you fetch data from any other source. However, this in my opinion only makes sense if you have a non-ORM based database, such as Redis or MongoDB for instance.