Object filter by one or more text choices ?

I have my one of my model choices like this :

    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')


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

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

return qs1 | qs2


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

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

It works, but is this the right approach ?

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

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

This is superb - thanks.

(You made a typo - MBB should be MBP)

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.