# query with Queryset

**URL:** <https://forum.djangoproject.com/t/query-with-queryset/2362>\
**Category:** Using Django\
**Created:** [May 10, 2020, 11:16am UTC](https://forum.djangoproject.com/t/query-with-queryset/2362 "2020-05-10T11:16:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Billonyohan](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@Billonyohan](https://forum.djangoproject.com/u/Billonyohan)\
**Post date:** [May 10, 2020, 11:16am UTC](https://forum.djangoproject.com/t/query-with-queryset/2362/1 "2020-05-10T11:16:59Z")

</div>

I would like get queryset with querysets and I don’t know if it’s possible

```
    get_color = request.GET.get('color') # I get 2

    if get_color == None:
        pass
    else:
        get_color_velo = Stock.objects.filter(color_id=get_color).values('velo_id') 
        # get_color_velo = <QuerySet [{'velo_id': 1}]>

        velo = Velo.objects.filter(pk=get_color_velo)
        #The QuerySet value for an exact lookup must be limited to one result using slicing.

```

But I can’t find the solution …

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 10, 2020, 11:26am UTC](https://forum.djangoproject.com/t/query-with-queryset/2362/2 "2020-05-10T11:26:59Z")

</div>

Is there any form of table relationship (ForeignKey) between Stock and Velo objects?

Is there more than one Stock object for any color? (In other words, are there cases where “get\_color\_velo” returns more than one result?)

Are you expecting only one, or possibly more than one, row from the Velo query?

The direct answer is yes it’s possible - but the specific answer depends upon your models.

---

<div class="post-metadata">

**Author:** ![Billonyohan](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@Billonyohan](https://forum.djangoproject.com/u/Billonyohan)\
**Post date:** [May 10, 2020, 12:30pm UTC](https://forum.djangoproject.com/t/query-with-queryset/2362/3 "2020-05-10T12:30:15Z")

</div>

Thank you for your answer

stock have two foreign key (color and velo) and yes get\_color\_velo may have more than one result

Velo may have more than one result too.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 10, 2020, 12:44pm UTC](https://forum.djangoproject.com/t/query-with-queryset/2362/4 "2020-05-10T12:44:31Z")

</div>

I believe you’re looking for the [‘in’ clause](https://docs.djangoproject.com/en/3.0/ref/models/querysets/#in).

So you might be looking for something like:

```auto
get_color_velo = Stock.objects.filter(color_id=get_color)
velo = Velo.objects.filter(pk__in=get_color_velo)

```

Note: Querysets are lazy, they don’t need to be evaluated before being referenced. By not using the values clause, you’re only going to execute one query (with embedded subquery) rather than two.

Ken

---

<div class="post-metadata">

**Author:** ![Billonyohan](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@Billonyohan](https://forum.djangoproject.com/u/Billonyohan)\
**Post date:** [May 10, 2020, 1:07pm UTC](https://forum.djangoproject.com/t/query-with-queryset/2362/5 "2020-05-10T13:07:28Z")

</div>

thank you very much i didn’t know pk\_\_in and it works perfectly

Thank you again for taking the time to answer me
