# Multiple User Types

**URL:** <https://forum.codewithmosh.com/t/multiple-user-types/20200>\
**Category:** Django\
**Created:** [May 12, 2023, 1:33pm UTC](https://forum.codewithmosh.com/t/multiple-user-types/20200 "2023-05-12T13:33:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anuragjain](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@anuragjain](https://forum.codewithmosh.com/u/anuragjain)\
**Post date:** [May 12, 2023, 1:33pm UTC](https://forum.codewithmosh.com/t/multiple-user-types/20200/1 "2023-05-12T13:33:28Z")

</div>

Hello,

I am confused on how to give permissions to different user types. In my app, we have multiple types such as builders and buyers. To access all projects listed on our website :

class ProjectViewSet(ModelViewSet):

def get\_queryset(self):  
if self.request and self.request.user.user\_type == ‘Builder’:  
builder= Builder.objects.filter(user\_id = self.request.user.id).first()  
return Project.objects.filter(builder= builder)  
return Project.objects.all()

**but this return error :**  
‘AnonymousUser’ object has no attribute ‘user\_type’

Pls confirm.

---

<div class="post-metadata">

**Author:** ![lucidlear](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/lucidlear/32/5477_2.png) [@lucidlear](https://forum.codewithmosh.com/u/lucidlear)\
**Post date:** [May 12, 2023, 10:30pm UTC](https://forum.codewithmosh.com/t/multiple-user-types/20200/2 "2023-05-12T22:30:18Z")

</div>

The error is occurring because the `AnonymousUser` object, which represents a user who is not authenticated, does not have a `user_type` attribute. Therefore, you cannot access the `user_type` attribute on the `self.request.user` object when the user is not authenticated.

To avoid this error, you can first check whether the user is authenticated before accessing the `user_type` attribute. Here is an updated version of the `get_queryset` method that includes this check:

```python
class ProjectViewSet(ModelViewSet):

    def get_queryset(self):
        if self.request.user.is_authenticated and self.request.user.user_type == 'Builder':
            builder = Builder.objects.filter(user_id=self.request.user.id).first()
            return Project.objects.filter(builder=builder)
        return Project.objects.all()

```

This updated version first checks whether the user is authenticated using the `is_authenticated` method on the `self.request.user` object. If the user is not authenticated, the `get_queryset` method simply returns all `Project` objects. If the user is authenticated and has a `user_type` attribute of “Builder”, the method filters the `Project` objects by the associated `Builder` object.

I hope this helps!
