Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django startswith query

Tags:

python

django

How would I do the following query?

SELECT * FROM title WHERE id LIKE '12345%'

What I currently have is:

Title.objects.get(id='12345')

Which obviously doesn't do the LIKE% (and the icontains does both). What would be the correct query here?

like image 964
David542 Avatar asked Jun 15 '26 10:06

David542


2 Answers

Title.objects.filter(id__startswith='12345')

https://docs.djangoproject.com/en/dev/ref/models/querysets/

like image 105
David542 Avatar answered Jun 17 '26 22:06

David542


You can do like this where code would be your startswith string by which you want to filter the table.

code = '12345' enter code hereTitle.objects.extra(where=["%s LIKE id||'%%'"], params=[code])

like image 23
vipul prajapati Avatar answered Jun 17 '26 22:06

vipul prajapati