使用values_list检索的空值

Drw*_*ite 2 python django

我根据表的一列(country_name)从数据库中检索了这个国家country列表:

list_countries = Country.objects.values_list('country_name', flat=True).distinct()
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

[u'', u'China', u'France', u'Germany', ...]
Run Code Online (Sandbox Code Playgroud)

数据库中的某些值为空.如何删除检索到的空白以便我获取检索到的结果只是country_name not null(cf country_name != '')?

Ade*_*taş 5

你可以使用Q对象,

from django.db.models import Q
list_countries = Country.objects.filter(~Q(country_name='')).values_list('country_name', flat=True).distinct()
Run Code Online (Sandbox Code Playgroud)