codkelden
codkelden

Reputation: 414

Django SQL LIKE on integer values?

I have a PostgreSQL table with id field (bigint). Can I somehow (possibly with ORM) perform check that id value ends with '00000' (e. g. "7500000", "7600000", but not "123456")? Thanks for any help.

Upvotes: 1

Views: 878

Answers (3)

karthikr
karthikr

Reputation: 99630

Django has an __endswith clause you could use.

qs = MyModel.objects.filter(myfield__endswith='00000') #id in your case

This fetches the queryset with all ids that end with 00000

Now, if you have an objects' instance at hand, and need to check if it ends with 00000, you dont need the ORM. You can do something like:

if str(myObject.id).endswith('00000'):

DEMO:

>>> str(10000000).endswith('00000')
True

Upvotes: 2

Sławek Kabik
Sławek Kabik

Reputation: 699

Try this: YourModel.objects.filter(id__endswith='00000'). It should also works on int fields :)

Upvotes: 2

mjl
mjl

Reputation: 197

If you work with numerical types, why don't you use numerical operations?

... WHERE id % 100000 = 0

should do what you want.

Upvotes: 1

Related Questions