django-select2: Can't set initial values of heavy widgets

I am having some trouble getting initial values set correctly in a Heavy Widget in django-select2

All works fine when using the light widget - but the heavy widgets (which otherwise get populated with data fine when being clicked on) don’t retain their previous data when re-loading the form data.

The issue seems to be in django_select2/forms.py

render_options(self, choices, selected_choices):

In my case selected choices correctly contains the key values of the previously selected items.

If I am reading the code right -

choices = {(k, v) for k, v in choices if k in selected_choices}

looks through the passed in choices values and creates a list of those which are also present in selected_choices.

That would work fine if choices contained the full list of possible values (which it does at the application level).

From what I can see render_options is called by django_select2 -

render(self, name, value, attrs=None, choices=()):

That expects a choices parameter otherwise it uses the default of no choices - which appears to be what is happening.

This code is called from (in django 1.8.3) django/forms/forms.py line 593

return force_text(widget.render(name, self.value(), attrs=attrs))

When it calls widget.render it doesn’t pass in any choices - so the default empty choices is used in the django_select2 code.

Is the issue with the django_select2 code? Or is it expecting to be called from something other than that line in django 1.8.3?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 18

Commits related to this issue

Most upvoted comments

@codingjoe , thanks for reply, I found a mistake in my code.