How to filter objects based on TextChoices ?

I’m using TextChoices to define Food category - see this example:

class CategoryChoice(models.TextChoices):
	# Fruits: T01-T50
    T01 = 'T01', 'Apple'
    T02 = 'T02', 'Banana'
    T03 = 'T03', 'Blackberries'
    T04 = 'T04', 'Cherries'
    T05 = 'T05', 'Cranberries'
    # Vegatables: T41-T60
    T41 = 'T41', 'Asparagus'
    T42 = 'T42', 'Broccoli'
    T43 = 'T43', 'Carrot'
	# Meat: T61-T99
    T61 = 'T61', 'Beef'
    T62 = 'T62', 'Lamb'
    T63 = 'T63', 'Pork'


class Food(models.Model):
    name = models.CharField(max_length=30)
    category = models.CharField(
        max_length=3,
        choices=CategoryChoice.choices,
        default=CategoryChoice.T01
    )

How can I properly query only Food objects which have category between T41 and T60 using Food.objects.filter() ?

If you’re sure that these lists sort properly and aren’t going to create any odd edge cases, you could use the __gte and __lte comparisons. (However, I personally discourage this type of comparison because according to sort order, ‘T415’ comes between ‘T41’ and ‘T42’.)

I would recommend using the __in comparison.

Thanks, yes it’s bad approach but it’s the fastest way to solve it :slight_smile: otherwise I would have to rethink the desing…