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

How to prepopulate the selected choices in HeavySelect2MultipleWidget and ModelMultipleChoiceField #4

Open
rmControls opened this issue Jul 22, 2020 · 7 comments
Labels
bug Something isn't working

Comments

@rmControls
Copy link

rmControls commented Jul 22, 2020

Goal
How to pre populate multiple selected choices in HeavySelect2MultipleWidget and in ModelMultipleChoiceField

Problem
When form reloaded with validation error, all the pre selected choices will vanish for HeavySelect2MultipleWidget, ModelMultipleChoiceField
also not finding a way to pre populate the selected choices when you load update form...

Code Snippet

class RequestRegion(forms.Form):
    region = forms.ModelMultipleChoiceField(queryset=Region.objects.all().order_by('region_id'),
                                            widget=Select2MultipleWidget)

class CustReqForm(forms.Form):

    #CustomerField(widget=CustomerHeavyWidget(data_view = 'customer_ajax', attrs={'data-minimum-input-length': 4, 'delay':200}))
    distributor = forms.CharField(
            widget=HeavySelect2MultipleWidget(data_view='customer_ajax',
                                              attrs={'data-minimum-input-length': 4, 'delay':200},
                                              ), required=False #queryset=Customer.objects.none()
    )
@rmControls rmControls added the question Further information is requested label Jul 22, 2020
@rmControls rmControls changed the title How to prepopulate the selected fields in HeavySelect2MultipleWidget, ModelMultipleChoiceField How to prepopulate the selected choices in HeavySelect2MultipleWidget, ModelMultipleChoiceField Jul 22, 2020
@rmControls rmControls changed the title How to prepopulate the selected choices in HeavySelect2MultipleWidget, ModelMultipleChoiceField How to prepopulate the selected choices in HeavySelect2MultipleWidget and ModelMultipleChoiceField Jul 22, 2020
@andrew-t34
Copy link

Hello. I also ran into a problem. When you need to edit form data, the transmitted data is not displayed. You have to re-select this data.

@simonkern
Copy link

I'm having the same issue. Have you been able to solve this in the meanwhile?

@codingjoe
Copy link
Owner

Hello. I also ran into a problem. When you need to edit form data, the transmitted data is not displayed. You have to re-select this data.

I am afraid this is a known issue. I would love to see someone come up with a solution tho.

@codingjoe codingjoe added bug Something isn't working and removed question Further information is requested labels Jul 8, 2021
@simonkern
Copy link

Do you happen to use django-crispyforms? In our case the issue was caused by the combinated use of select2 and crispy forms.

@andrew-t34
Copy link

I have the same issue. Deleting django-crispyforms didn't help.

@damarro3
Copy link

Has been any improvement in this issue?

@Kagee
Copy link

Kagee commented May 12, 2024

Select2 want the preselected values as option-elements with selected="selected": https://select2.org/data-sources/ajax#default-pre-selected-values

I did this the following way:

  1. Make the views get_form add the values I want prepopulated as option values to the ModelSelect2TagWidget of the field:
    https://github.com/Kagee/homelab-organizer/blob/0dd3b4279fe1320178c92a0c5a01f54a287f7ee1/hlo/views/stockitems.py#L167
  2. Override create_option in the widget to always set selected to "selected", as i could not get anaything like initial values to work. This will work as all the options are the options from step 1. that we want selected: https://github.com/Kagee/homelab-organizer/blob/0dd3b4279fe1320178c92a0c5a01f54a287f7ee1/hlo/forms.py#L18

If anyone have any better suggestions after seeing my workaround, please share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants