H C
H C

Reputation: 1200

Queryset filter for part of a number

I have a list of codes:

21000000 Code 1
21000001 Code 2
22000000 Code 3
22000001 Code 4
22002100 Code 5

The model for the code is:

class Category(models.Model):
    code_id = models.CharField(max_length=50)

I would like to filter only the codes that start with 2100XXXX. When I use startswith, it doesn't seem to recognize number. What is the proper way to right a queryset call to only filter for 2100XXXX. Currently, my code is:

DB.objects.filter(code__startswith='2100')

This should pick up Code 1 and 2 and no others.

Thanks

Upvotes: 0

Views: 87

Answers (1)

karthikr
karthikr

Reputation: 99680

You have named your field code_id and your filter is: code__startswith

This should work:

DB.objects.filter(code_id__startswith='2100')

Upvotes: 1

Related Questions