{"action":"reopened","issue":{"url":"https://api.github.com/repos/applegrew/django-select2/issues/436","repository_url":"https://api.github.com/repos/applegrew/django-select2","labels_url":"https://api.github.com/repos/applegrew/django-select2/issues/436/labels{/name}","comments_url":"https://api.github.com/repos/applegrew/django-select2/issues/436/comments","events_url":"https://api.github.com/repos/applegrew/django-select2/issues/436/events","html_url":"https://github.com/applegrew/django-select2/issues/436","id":271905792,"number":436,"title":"Chained field ignores \"selected\" attribute.","user":{"login":"eriktelepovsky","id":2685808,"avatar_url":"https://avatars3.githubusercontent.com/u/2685808?v=4","gravatar_id":"","url":"https://api.github.com/users/eriktelepovsky","html_url":"https://github.com/eriktelepovsky","followers_url":"https://api.github.com/users/eriktelepovsky/followers","following_url":"https://api.github.com/users/eriktelepovsky/following{/other_user}","gists_url":"https://api.github.com/users/eriktelepovsky/gists{/gist_id}","starred_url":"https://api.github.com/users/eriktelepovsky/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eriktelepovsky/subscriptions","organizations_url":"https://api.github.com/users/eriktelepovsky/orgs","repos_url":"https://api.github.com/users/eriktelepovsky/repos","events_url":"https://api.github.com/users/eriktelepovsky/events{/privacy}","received_events_url":"https://api.github.com/users/eriktelepovsky/received_events","type":"User","site_admin":false},"labels":[{"id":10026586,"url":"https://api.github.com/repos/applegrew/django-select2/labels/question","name":"question","color":"cc317c","default":true}],"state":"open","locked":false,"assignee":{"login":"codingjoe","id":1772890,"avatar_url":"https://avatars1.githubusercontent.com/u/1772890?v=4","gravatar_id":"","url":"https://api.github.com/users/codingjoe","html_url":"https://github.com/codingjoe","followers_url":"https://api.github.com/users/codingjoe/followers","following_url":"https://api.github.com/users/codingjoe/following{/other_user}","gists_url":"https://api.github.com/users/codingjoe/gists{/gist_id}","starred_url":"https://api.github.com/users/codingjoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codingjoe/subscriptions","organizations_url":"https://api.github.com/users/codingjoe/orgs","repos_url":"https://api.github.com/users/codingjoe/repos","events_url":"https://api.github.com/users/codingjoe/events{/privacy}","received_events_url":"https://api.github.com/users/codingjoe/received_events","type":"User","site_admin":false},"assignees":[{"login":"codingjoe","id":1772890,"avatar_url":"https://avatars1.githubusercontent.com/u/1772890?v=4","gravatar_id":"","url":"https://api.github.com/users/codingjoe","html_url":"https://github.com/codingjoe","followers_url":"https://api.github.com/users/codingjoe/followers","following_url":"https://api.github.com/users/codingjoe/following{/other_user}","gists_url":"https://api.github.com/users/codingjoe/gists{/gist_id}","starred_url":"https://api.github.com/users/codingjoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codingjoe/subscriptions","organizations_url":"https://api.github.com/users/codingjoe/orgs","repos_url":"https://api.github.com/users/codingjoe/repos","events_url":"https://api.github.com/users/codingjoe/events{/privacy}","received_events_url":"https://api.github.com/users/codingjoe/received_events","type":"User","site_admin":false}],"milestone":null,"comments":5,"created_at":"2017-11-07T16:53:22Z","updated_at":"2017-11-16T18:40:26Z","closed_at":null,"author_association":"NONE","body":"Hi. I am using the **dependent fields** functionality and I am trying to preselect the options instead of returning the filtered queryset. I have a custom ajax view which returns the correct data according to my needs in the following format defined by [select2 API](https://select2.org/data-sources/formats#selected-and-disabled-options):\r\n\r\n```\r\n{\r\n    \"more\": false, \r\n    \"results\": [\r\n        {\r\n            \"text\": \"Option 1\", \r\n            \"selected\": false, \r\n            \"id\": 5\r\n        }, \r\n        {\r\n            \"text\": \"Option 2\", \r\n            \"selected\": true,\r\n            \"id\": 6\r\n        }\r\n    ]\r\n}\r\n```\r\n\r\nMy code:\r\n\r\n```\r\nclass AutoSelectedResponseView(AutoResponseView):\r\n    def get_queryset(self):\r\n        \"\"\"Override queryset to return all results, not only dependent\"\"\"\r\n        whole_qs = self.widget.filter_queryset(self.term, self.queryset)\r\n        return whole_qs\r\n\r\n    def get(self, request, *args, **kwargs):\r\n        \"\"\"\r\n        Return a :class:`.django.http.JsonResponse`.\r\n\r\n        Example::\r\n\r\n            {\r\n                'results': [\r\n                    {\r\n                        'text': \"foo\",\r\n                        'id': 123,\r\n                        'selected': false\r\n                    }\r\n                ],\r\n                'more': true\r\n            }\r\n\r\n        \"\"\"\r\n\r\n        self.widget = self.get_widget_or_404()\r\n        self.term = kwargs.get('term', request.GET.get('term', ''))\r\n        self.object_list = self.get_queryset()\r\n        context = self.get_context_data()\r\n\r\n        # dependent/filtered results\r\n        dependent_qs = super(AutoSelectedResponseView, self).get_queryset()\r\n\r\n        return JsonResponse({\r\n            'results': [\r\n                {\r\n                    'text': self.widget.label_from_instance(obj),\r\n                    'id': obj.pk,\r\n                    'selected': obj in dependent_qs  # selected if object is dependent\r\n                }\r\n                for obj in context['object_list']\r\n                ],\r\n            'more': context['page_obj'].has_next()\r\n        })\r\n```\r\n\r\nUnfortunately, the value is never pre-selected. Any ideas why? Thank you. "}}
{"actor":{"display_login":"codingjoe"}}