简体   繁体   中英

Using .filter in Django to see if title contains a certain string?

My code is:

posts = Listing.objects.filter(title=q, is_live=1)

I need the equivalent of:

posts = Listing.objects.filter(q in title, is_live=1)

How can I just determine if a certain bit of text exists and isn't exactly equal to the query?

Try this:

posts = Listing.objects.filter(title__contains=q, is_live=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM