简体   繁体   中英

__contains filter doesn't work on mysql, works on local SQLite db

I have following code snippet:

profiles_list = Profile.objects.filter(
    company=request.user.company,
)
search_query = None
search_key = None
if request.method == 'POST':
    search_key = request.POST.get('skey')
    search_query = request.POST.get('squery', None)
    profiles_list = profiles_list.filter(
        **{'%s__contains' % search_key:search_query}
    )

On my local dev machine with SQLite database, if I type in search_query, for example "Owal" it returns me a record where search_key contains "Owal", so I get for example "Kowalski".

I tried it on production server with MySQL and it doesn't work. Do you know why?

From Django documentation :

SQLite doesn't support case-sensitive LIKE statements; contains acts like icontains for SQLite. See the database note for more information.

Suggestion: Use %s__icontains instead.

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