{"action":"created","issue":{"url":"https://api.github.com/repos/applegrew/django-select2/issues/222","labels_url":"https://api.github.com/repos/applegrew/django-select2/issues/222/labels{/name}","comments_url":"https://api.github.com/repos/applegrew/django-select2/issues/222/comments","events_url":"https://api.github.com/repos/applegrew/django-select2/issues/222/events","html_url":"https://github.com/applegrew/django-select2/issues/222","id":115194823,"number":222,"title":"Can't set initial values of heavy widgets","user":{"login":"Peter-Gr","id":12927371,"avatar_url":"https://avatars.githubusercontent.com/u/12927371?v=3","gravatar_id":"","url":"https://api.github.com/users/Peter-Gr","html_url":"https://github.com/Peter-Gr","followers_url":"https://api.github.com/users/Peter-Gr/followers","following_url":"https://api.github.com/users/Peter-Gr/following{/other_user}","gists_url":"https://api.github.com/users/Peter-Gr/gists{/gist_id}","starred_url":"https://api.github.com/users/Peter-Gr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Peter-Gr/subscriptions","organizations_url":"https://api.github.com/users/Peter-Gr/orgs","repos_url":"https://api.github.com/users/Peter-Gr/repos","events_url":"https://api.github.com/users/Peter-Gr/events{/privacy}","received_events_url":"https://api.github.com/users/Peter-Gr/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/applegrew/django-select2/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-11-05T02:22:18Z","updated_at":"2015-11-05T20:05:17Z","closed_at":null,"body":"I am having some trouble getting initial values set correctly in a Heavy Widget in django-select2\r\n\r\nAll 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.\r\n\r\nThe issue seems to be in django_select2/forms.py\r\nrender_options(self, choices, selected_choices):\r\n\r\nIn my case selected choices correctly contains the key values of the previously selected items.\r\n\r\nIf I am reading the code right -\r\nchoices = {(k, v) for k, v in choices if k in selected_choices}\r\n\r\nlooks through the passed in choices values and creates a list of those which are also present in selected_choices.\r\n\r\nThat would work fine if choices contained the full list of possible values (which it does at the application level).\r\n\r\nFrom what I can see render_options is called by django_select2 - render(self, name, value, attrs=None, choices=()):\r\n\r\nThat expects a choices parameter otherwise it uses the default of no choices - which appears to be what is happening.\r\n\r\nThis code is called from (in django 1.8.3) django/forms/forms.py line 593\r\nreturn force_text(widget.render(name, self.value(), attrs=attrs))\r\n\r\nWhen it calls widget.render it doesn't pass in any choices - so the default empty choices is used in the django_select2 code.\r\n\r\nIs 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?\r\n"},"comment":{"url":"https://api.github.com/repos/applegrew/django-select2/issues/comments/154174872","html_url":"https://github.com/applegrew/django-select2/issues/222#issuecomment-154174872","issue_url":"https://api.github.com/repos/applegrew/django-select2/issues/222","id":154174872,"user":{"login":"Peter-Gr","id":12927371,"avatar_url":"https://avatars.githubusercontent.com/u/12927371?v=3","gravatar_id":"","url":"https://api.github.com/users/Peter-Gr","html_url":"https://github.com/Peter-Gr","followers_url":"https://api.github.com/users/Peter-Gr/followers","following_url":"https://api.github.com/users/Peter-Gr/following{/other_user}","gists_url":"https://api.github.com/users/Peter-Gr/gists{/gist_id}","starred_url":"https://api.github.com/users/Peter-Gr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Peter-Gr/subscriptions","organizations_url":"https://api.github.com/users/Peter-Gr/orgs","repos_url":"https://api.github.com/users/Peter-Gr/repos","events_url":"https://api.github.com/users/Peter-Gr/events{/privacy}","received_events_url":"https://api.github.com/users/Peter-Gr/received_events","type":"User","site_admin":false},"created_at":"2015-11-05T20:05:17Z","updated_at":"2015-11-05T20:05:17Z","body":"Thanks for any help on this. Yep I am using the heavy control as I need the web-page to go off to the database to get a particular set of options based on certain criteria on the page. That side of it all works fine \r\n\r\nThere are a number of form controls on the page set up as:\r\n\r\ndef CreateMultiChoiceField(label_text, choice_values):\r\n    return MultipleChoiceField(label = label_text,\r\n                                      choices = choice_values,\r\n                                      required=False,\r\n                                      widget=HeavySelect2MultipleWidget(attrs={'class':'form-control searchable-control',\r\n                                                                          'data-placeholder' : 'All'},\r\n                                                                        data_url = 'updateselections'))\r\n\r\n\r\nOn creation choice_values is pre-populated with the set of all possible values (20 or 30 values). This are subsequently not used, as the choices are populated through an ajax query, but it looks like they are used in the code noted above, with the noted issues.\r\n\r\nThe view method which displays the page is pretty standard:\r\n\r\ndef index(request):\r\n    if request.method == 'POST':\r\n        form = FilterForm(request.POST)\r\n        \r\n        if form.is_valid():\r\n            return render_to_response('graphs/index.html',{'durationchart': GetChart(form.cleaned_data), \r\n                                                              'displaygraph' : True,\r\n                                                              'form'         : form},\r\n                              context_instance=RequestContext(request))\r\n    else:\r\n        form = FilterForm()\r\n    \r\n    return render_to_response('graphs/index.html',{'displaygraph' : False,\r\n                                                      'form'         : form},\r\n                              context_instance=RequestContext(request))\r\n\r\nThe part which isn't working is the following flow.\r\n\r\n1 - User opens web page with blank form data.\r\n2 - User selected some items in the multi-select-heavy widgets.\r\n3 - User presses submit to process the form data.\r\n4 - This causes the page to be re-displayed with the previously filled in form data at the top and a graph displayed at the bottom based on the data which was entered into the form,\r\n5 - the re-displayed page should have all the form fields filled in with the data which they contained before the form was submitted, so that the user can tweak the form values if they want and then re-display the graph with different options selected.\r\n\r\nThis all works perfectly for the other controls on the form, and even for the Light widget, but the heavy widget is not populating the option tags like:\r\n\r\n<option value=\"2\">DUPLICATE</option>\r\n<option value=\"1\" selected=\"selected\">NORMAL</option>\r\n<option value=\"3\">OUTAGE</option>\r\n\r\nFrom what I can tell this is required in order to have the select field display some pre-selected values on page-load.\r\n\r\nThis logic is contained in the django_select2 code:\r\n\r\n    def render_options(self, choices, selected_choices):\r\n        \"\"\"Render only selected options.\"\"\"\r\n        output = ['<option></option>' if not self.is_required else '']\r\n        choices = {(k, v) for k, v in choices if k in selected_choices}\r\n        selected_choices = {force_text(v) for v in selected_choices}\r\n        for option_value, option_label in choices:\r\n            output.append(self.render_option(selected_choices, option_value, option_label))\r\n        return '\\n'.join(output)\r\n\r\nDue to the issue I noted above, it looks like choices is blank when this is called and thus no option values are rendered to the HTML...."}}