简体   繁体   中英

Django: Elegant way to lookup a non-empty string

I have a model Chair with a blank-able CharField called wood_type .

I want to filter all the chairs with a wood_type which is not '' .

What's an elegant way to do it with Django?

I can think of this:

Chair.objects.filter(~django.db.models.Q(wood_type=''))

Or this:

Chair.objects.filter(wood_type__regex='(.|\n)+')

But they're ugly as hell. Is there a more elegant way?

Chair.objects.exclude(wood_type='')应该可以解决问题。

排除空字符串

Chair.objects.exclude(wood_type='')

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