Monday, 8 July 2019

Compare string alphabetically django db

I have a following model:

class Page(Model):
      book = ForeignKey(Book, on_delete=CASCADE)
      page = IntegerField()
      start = CharField(max_length=350, db_index=True)
      end = CharField(max_length=350, db_index=True)

How do I query DB in order to get pages that "contain" a given word?

page1 = Page.objects.create(start='beaver', end='brother')
page2 = Page.objects.create(start='boy', end='brother')

  • Page.objects.filter("breast" between start and end) should return page1 and page2.
  • Page.objects.filter("beast" between start and end) should return nothing.
  • Page.objects.filter("block" between start and end) should return the only page1, since block is alphabetically after beaver and before brother.

Search should be case-insensitive.

So, I need to write a query that fetches all rows, where start is alphabetically "smaller" than the given word and end is alphabetically "larger" than the given word.



from Compare string alphabetically django db

No comments:

Post a Comment