{"action":"closed","issue":{"url":"https://api.github.com/repos/carltongibson/django-filter/issues/929","repository_url":"https://api.github.com/repos/carltongibson/django-filter","labels_url":"https://api.github.com/repos/carltongibson/django-filter/issues/929/labels{/name}","comments_url":"https://api.github.com/repos/carltongibson/django-filter/issues/929/comments","events_url":"https://api.github.com/repos/carltongibson/django-filter/issues/929/events","html_url":"https://github.com/carltongibson/django-filter/issues/929","id":335151820,"node_id":"MDU6SXNzdWUzMzUxNTE4MjA=","number":929,"title":"No documentation or examples on changing the queryset for a ModelChoiceField or choices for a ChoiceField","user":{"login":"aidanlister","id":54896,"node_id":"MDQ6VXNlcjU0ODk2","avatar_url":"https://avatars0.githubusercontent.com/u/54896?v=4","gravatar_id":"","url":"https://api.github.com/users/aidanlister","html_url":"https://github.com/aidanlister","followers_url":"https://api.github.com/users/aidanlister/followers","following_url":"https://api.github.com/users/aidanlister/following{/other_user}","gists_url":"https://api.github.com/users/aidanlister/gists{/gist_id}","starred_url":"https://api.github.com/users/aidanlister/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aidanlister/subscriptions","organizations_url":"https://api.github.com/users/aidanlister/orgs","repos_url":"https://api.github.com/users/aidanlister/repos","events_url":"https://api.github.com/users/aidanlister/events{/privacy}","received_events_url":"https://api.github.com/users/aidanlister/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-06-24T05:12:54Z","updated_at":"2018-07-13T10:45:14Z","closed_at":"2018-07-13T10:45:14Z","author_association":"NONE","body":"Spent a few hours trying to get this working ... can we have some documentation about changing choices and querysets in __init__?\r\n\r\nHere's what I was trying to do ... still didn't work out the `extra['choices']` where it depended on the `qs`\r\n\r\n```python\r\nclass PropertyFilterSet(django_filters.FilterSet):\r\n    state = django_filters.ChoiceFilter(label=\"State\", name='address__state', choices=EMPTY_CHOICES)\r\n    manager = django_filters.ModelChoiceFilter(label=\"Manager\", name='client', to_field_name='name', queryset=Client.objects.none())\r\n    bankbrand = django_filters.ChoiceFilter(label=\"Bank Brand\", name='extra_fields__commbank_brand', choices=EMPTY_CHOICES)\r\n    portfolio = django_filters.ChoiceFilter(label=\"Portfolio\", name='extra_fields__commbank_portfolio', choices=EMPTY_CHOICES)\r\n\r\n    def __init__(self, *args, **kwargs):\r\n        # can't access qs here? calls is_bound\r\n        # self.declared_filters['state'].extra['choices'] = EMPTY_CHOICES + list(self.qs.order_by().values_list('address__state', 'address__state').distinct())\r\n        self.declared_filters['state'].extra['choices'] = EMPTY_CHOICES + [('VIC', 'VIC'), ('NSW', 'NSW'), ('ACT', 'ACT'), ('WA', 'WA'), ('TAS', 'TAS')]\r\n        self.declared_filters['bankbrand'].extra['choices'] = EMPTY_CHOICES + [(o['value'], o['label']) for o in ExtraField.objects.get(name='commbank_brand').field_settings['choices']]\r\n        self.declared_filters['portfolio'].extra['choices'] = EMPTY_CHOICES + [(o['value'], o['label']) for o in ExtraField.objects.get(name='commbank_mainusage').field_settings['choices']]\r\n        \r\n        super().__init__(*args, **kwargs)\r\n        # modelfieldset can be done after init? choicefield has no change at this point\r\n        valid_clients = Client.objects.active().filter(Q(clientgroup=CBA_CLIENTGROUP_ID) | Q(clientgroup__parent_clientgroup=CBA_CLIENTGROUP_ID))\r\n        self.filters['manager'].queryset = valid_clients\r\n\r\n    class Meta:\r\n        model = Property\r\n        fields = (\r\n            'state',\r\n            'manager',\r\n            'bankbrand',\r\n            'portfolio',\r\n        )\r\n\r\n```"}}
{"actor":{"display_login":"carltongibson"}}