-1

I was writing a statement containing this where-clause in peewee:

.where(some_string.startswith(Record.text_field))

This expression raises TypeError: startswith first arg must be str or a tuple of str, not CharField. some_string is just a normal string, therefore startswith is the method from the standard lib, not the one that is provided by peewee.

So it is clear, why that happens, but what is the correct way to write this expression?

Max Tet
  • 735
  • 1
  • 8
  • 16

1 Answers1

-1

You've either got things very backwards or do not understand how Peewee works. If you have a string ("some_string") how do you expect the Python interpreter to perform a startswith test on a value stored in a db column?

The correct approach is to use Record.text_field.startswith(some_string) which translates into

WHERE "record"."text_field" LIKE 'value in some string%'
coleifer
  • 24,887
  • 6
  • 60
  • 75
  • As I wrote, it is clear why the error is raised. Of course it cannot work that way. But your proposed solution seems to be the one that is backwards. The `text_field` should be a prefix of `some_string` not the other way around. `a.startswith(b)` does not equal `b.startswith(a)`. – Max Tet Dec 08 '19 at 13:12