python – How to filter and get an “object” instead of a “quesryset” in Django Rest Framework


I am trying to filter the users by passing the email_id through URL by passing email_id and getting that particular user. I tried doing this using django-filters but I got a queryset with one object inside it but instead I need the object itself.

here’s the model –

class User(models.Model):
    """This is the Users model where all users data will be saved"""
    first_name    = models.CharField(max_length=20)
    last_name     = models.CharField(max_length=20)
    email_id      = models.EmailField(max_length=100, default="")
    city          = models.CharField(max_length=20, blank=True)
    state         = models.CharField(max_length=50, blank=True)
    country       = models.CharField(max_length=20, blank=True)
    zip_code      = models.CharField(max_length=6, blank=True)
    mobile_number = models.CharField(max_length=10, blank=True)
    birthdate     = models.DateField(blank=True, null=True)
    photo_url     = models.URLField(max_length=255, blank=True)
    gender        = models.ForeignKey(Gender, on_delete=models.CASCADE, null=True, blank=True, 
    related_name='user_gender')
    joined        = models.DateTimeField(auto_now_add=True, blank=True, null=True)


    def __str__(self):
        return self.first_name + ' ' + self.last_name

I tried using this viewset but it gives me a queryset instead of an object and I coudn’t put my mind around on it.

class UserFilter(filters.FilterSet):

    class Meta: 
        model = User
        fields = {
            'email_id': ('iexact')
        }

class UserViewSet(viewsets.ModelViewSet):
 
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_backends = (DjangoFilterBackend)
    filterset_class = UserFilter

This is the url I am currently passing –

http://127.0.0.1:8000/api/v1/users/?email_id__iexact=somemail@gmail.com