# Filter Model Choice Field's custom queryset inside a Form

**URL:** <https://forum.djangoproject.com/t/filter-model-choice-fields-custom-queryset-inside-a-form/45581>\
**Category:** Forms & APIs\
**Created:** [July 26, 2026, 5:17am UTC](https://forum.djangoproject.com/t/filter-model-choice-fields-custom-queryset-inside-a-form/45581 "2026-07-26T05:17:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![i0Ta07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/i0ta07/32/32930_2.png) [@i0Ta07](https://forum.djangoproject.com/u/i0Ta07)\
**Post date:** [July 26, 2026, 5:17am UTC](https://forum.djangoproject.com/t/filter-model-choice-fields-custom-queryset-inside-a-form/45581/1 "2026-07-26T05:17:49Z")

</div>

I am using a one to one field from another model and using model choice field inside the generic forms.Form but I want to filter with some parameter and that parameter is available to me in the class based view using kwargs[‘org\_id’]. How do I access kwargs[‘org\_id’] inside the form?

```auto
class CreateAdminForm(forms.Form):
    users = forms.ModelChoiceField(queryset=OrgMembership.objects.filter(org = given_org_from_kwargs))

class CreateAdmins(LoginRequiredMixin,TeacherRequiredMixin,OwnerAdminRequired,View):
    template_name = "orgs/create_admin.html"
    form_class = CreateAdminForm

    def get(self,request, *args, **kwargs):
        form = self.form_class()
        context = {'form':form,"org_id":kwargs['org_id']} # sending to the view also
        if request.htmx:
            return render(request,template_name="orgs/create_admin.html#create-admin",context=context)
        return render(request,template_name=self.template_name,context=context)
    

```

---

<div class="post-metadata">

**Author:** ![villager](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/villager/32/24305_2.png) [@villager](https://forum.djangoproject.com/u/villager)\
**Post date:** [July 26, 2026, 6:21pm UTC](https://forum.djangoproject.com/t/filter-model-choice-fields-custom-queryset-inside-a-form/45581/2 "2026-07-26T18:21:10Z")

</div>

I would set the queryset in the Form’s constructor.

```python
class CreateAdminForm(forms.Form):
    users = forms.ModelChoiceField(queryset=None)

    def __init__ (self, *args, org_id, **kwargs):
        super(). __init__ (*args, **kwargs)
        users = OrgMembership.objects.filter(org_id=org_id)
        self.fields["users"].queryset = users

```

Then in the `get()` function:

```python
form = self.form_class(org_id=kwargs["org_id"])

```

---

<div class="post-metadata">

**Author:** ![i0Ta07](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/i0ta07/32/32930_2.png) [@i0Ta07](https://forum.djangoproject.com/u/i0Ta07)\
**Post date:** [July 26, 2026, 8:58pm UTC](https://forum.djangoproject.com/t/filter-model-choice-fields-custom-queryset-inside-a-form/45581/3 "2026-07-26T20:58:06Z")

</div>

Thanks for the help.
