0

I'm trying to get a list of results where the given string address is in models.Listing.address. My code at the moment is:

@app.route('/search/<address>')
def search(address):
    results = models.Listing.select().where(address << models.Listing.address)

    return render_template('search.html', results=results)

For example I might pass in 39 Main Road and would want it to select the records where 39 Main Road is in the address field of a given listing (full address being 39 Main Road RICHMOND, NSW, Australia)

However, my current code errors with: TypeError: unsupported operand type(s) for <<: 'str' and 'CharField'

I've tried converting models.Listing.address to str but that just returns unsupported operand type(s) for <<: 'str' and 'str'

I would use address in models.Listing.address but as per the docs and this answer it is neccesarry to use the << operator.

Furthermore, i've tried using address.in_(models.Listing.address) which string doesn't have the property for...

Is it because i'm not comparing a CharField with a CharField? If so, how do I compare a string with a CharField? I've tried changing the CharField to a string but I can't use in with peewee because as @coleifer said in a comment in this answer:

Python always coerces the return value of x in y to a boolean, necessitating the use of the << operator.

Thanks in advance!

Community
  • 1
  • 1
TomHill
  • 614
  • 1
  • 10
  • 26

1 Answers1

1

Well... as I was soon to find out:

x << y is for checking if the record x is in a list or query of records y

whereas the functionality I was trying to achieve is done by:

.where(models.Listing.address.contains(address))

So there you go, future me who will inevitably do this again ;)

TomHill
  • 614
  • 1
  • 10
  • 26