# How to access user.id in forms.py?

**URL:** https://forum.djangoproject.com/t/how-to-access-user-id-in-forms-py/12413
**Category:** Forms & APIs
**Created:** [March 2, 2022, 1:59pm UTC](https://forum.djangoproject.com/t/how-to-access-user-id-in-forms-py/12413 "2022-03-02T13:59:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![StijnWauterss](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stijnwauterss/32/5475_2.png) [@StijnWauterss](https://forum.djangoproject.com/u/StijnWauterss)
#### Post date: [March 2, 2022, 1:59pm UTC](https://forum.djangoproject.com/t/how-to-access-user-id-in-forms-py/12413/1 "2022-03-02T13:59:00Z")

</div>

I want to be able to access the id of the signed in user in my forms to make a certain query for the Select2 field.

Unfortunately I don’t know how to get the user.id in the forms.py

This is what I’m trying currently in my forms.py:

```auto
class InsertVisitRepresentativeForm(ModelForm):
    class Meta:
        model = VisitsRepresentatives
        fields = ' __all__'

        widgets = { 
            'customer' : s2forms.ModelSelect2Widget(
                model=VisitsRepresentatives,
                attrs={'data-placeholder':_("Customer"), 'data-minimum-input-length': 1},
                search_fields = ['contactPerson__icontains']
            ),
        }

    def __init__ (self, *args, **kwargs):
        super(InsertVisitRepresentativeForm, self). __init__ (*args, **kwargs)

        print(self.user)

        postalRepresentativeObject = PostalRepresentative.objects.get(representative = self.user)

        postalCodes = postalRepresentativeObject.postalCodes

        self.fields["customer"].queryset = Customers.objects.filter(Q(postalcode__in = postalCodes) | Q(postalcode__gte = postalRepresentativeObject.beginPostalRange) & Q(postalcode__lte = postalRepresentativeObject.endPostalRange))

```

This does not work, so I’m wondering how to get the user id in a ModelForm?

---

<div class="post-metadata">

### Author: ![axeljacobs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/axeljacobs/32/3939_2.png) [@axeljacobs](https://forum.djangoproject.com/u/axeljacobs)
#### Post date: [March 2, 2022, 2:15pm UTC](https://forum.djangoproject.com/t/how-to-access-user-id-in-forms-py/12413/2 "2022-03-02T14:15:17Z")

</div>

Hi,

I read something about accessing the user and pass it to a form…

here is the link:

> **[Django: how to pass the user object into form classes](https://medium.com/analytics-vidhya/django-how-to-pass-the-user-object-into-form-classes-ee322f02948c)**
>
> Yesterday, I published a tutorial on how to build forms with ManyToMany fields. The back-end work was almost complete, except for a bug…

Hope it will help.

---

<div class="post-metadata">

### Author: ![StijnWauterss](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/stijnwauterss/32/5475_2.png) [@StijnWauterss](https://forum.djangoproject.com/u/StijnWauterss)
#### Post date: [March 2, 2022, 2:27pm UTC](https://forum.djangoproject.com/t/how-to-access-user-id-in-forms-py/12413/3 "2022-03-02T14:27:09Z")

</div>

Yes I do believe this works! Thanks, how did you find this so fast?

---

<div class="post-metadata">

### Author: ![axeljacobs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/axeljacobs/32/3939_2.png) [@axeljacobs](https://forum.djangoproject.com/u/axeljacobs)
#### Post date: [March 2, 2022, 2:41pm UTC](https://forum.djangoproject.com/t/how-to-access-user-id-in-forms-py/12413/4 "2022-03-02T14:41:39Z")

</div>

> [@StijnWauterss](#):
>
> Thanks, how did you find this so fast

you’re welcome… I’m glad that it helped you…

I had a similar issue the other day… I remembered that I read about this…
