# Object filter by one or more text choices ?

**URL:** <https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975>\
**Category:** Using Django\
**Created:** [May 20, 2021, 4:53am UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975 "2021-05-20T04:53:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [May 20, 2021, 4:53am UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975/1 "2021-05-20T04:53:14Z")

</div>

I have my one of my model choices like this :

```auto
    class TypeOfComputer(models.TextChoices):
        MacBookPro = 'MBP', _('MacBook Pro')
        MacBookAir = 'MBA', _('MacBook Air')
        iMac = 'iMAC', _('iMac')
        iMacPro = 'iMACPRO', _('iMac Pro')
        MacMini = 'MINI', _('Mac Mini')
        MacPro = 'MACPRO', _('Mac Pro')

```

Ho do I filter by one or more choices ?  
`products = Product.objects.all().filter(type='MBP' or type='MBA').order_by('created')`

---

<div class="post-metadata">

**Author:** ![DanielGnzlzVll](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danielgnzlzvll/32/4585_2.png) [@DanielGnzlzVll](https://forum.djangoproject.com/u/DanielGnzlzVll)\
**Post date:** [May 20, 2021, 5:13am UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975/2 "2021-05-20T05:13:43Z")

</div>

Hi,

You can use `Q` objects or boolean operators on the querysets.

```python
qs1 = model.objects.filter(type='MBA')
qs2 = model.objects.filter(type='MBB')

return qs1 | qs2

```

[https://docs.djangoproject.com/en/3.2/topics/db/queries/#complex-lookups-with-q-objects](https://docs.djangoproject.com/en/3.2/topics/db/queries/#complex-lookups-with-q-objects)

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [May 20, 2021, 5:39am UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975/3 "2021-05-20T05:39:38Z")

</div>

Thanks, this is new. I assembled this from the docs.

```auto
    query = Q(type='MBP') | Q(type='MBA'))
    products = Product.objects.all().filter(query).order_by('created')

```

It works, but is this the right approach ?

---

<div class="post-metadata">

**Author:** ![DanielGnzlzVll](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/danielgnzlzvll/32/4585_2.png) [@DanielGnzlzVll](https://forum.djangoproject.com/u/DanielGnzlzVll)\
**Post date:** [May 20, 2021, 6:00am UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975/4 "2021-05-20T06:00:31Z")

</div>

I forget told you that you also can use the `IN` operator

```python
return model.objects.filter(type__in=['MBA', 'MBB']) 

```

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [May 20, 2021, 6:19am UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975/5 "2021-05-20T06:19:08Z")

</div>

> [@DanielGnzlzVll](#):
>
> `type__in=['MBA', 'MBB']`

This is superb - thanks.

(You made a typo - MBB should be MBP)

---

<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 20, 2021, 12:43pm UTC](https://forum.djangoproject.com/t/object-filter-by-one-or-more-text-choices/7975/6 "2021-05-20T12:43:27Z")

</div>

Either one of those solutions are fine.

When you reach the point where you’ve got a few thousand Product objects, you’ll want to create an index on the `type` column. (By the way, there’s a built-in function `type` in Python. I generally suggest to people that they don’t create possible conflict or confusion between built-in functions and objects and column names.)

Get to the point where you’ve got a couple hundred thousand Product objects, and it might be worth benchmarking each of the two to determine which performs better.
