Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-按列表中键的一部分进行筛选_Python_Django_Django Models - Fatal编程技术网

Python Django-按列表中键的一部分进行筛选

Python Django-按列表中键的一部分进行筛选,python,django,django-models,Python,Django,Django Models,我在django 1.8表中有一些数据: “键”列值=“a.2”、“b.4”、“c.6” 我想通过[“a”,“b”]过滤它,忽略点后面的部分 Blog.objects.filter(key__startswith=['a','b']) 一无所获 Blog.objects.filter(key__contains=['a','b']) Blog.objects.filter(key__in=['a%','b%']) 一无所获 Blog.objects.filter(key__contains

我在django 1.8表中有一些数据: “键”列值=“a.2”、“b.4”、“c.6” 我想通过[“a”,“b”]过滤它,忽略点后面的部分

Blog.objects.filter(key__startswith=['a','b'])
一无所获

Blog.objects.filter(key__contains=['a','b'])
Blog.objects.filter(key__in=['a%','b%'])
一无所获

Blog.objects.filter(key__contains=['a','b'])
Blog.objects.filter(key__in=['a%','b%'])
一无所获

Blog.objects.filter(key__contains=['a','b'])
Blog.objects.filter(key__in=['a%','b%'])

与循环相比,有没有一种有效的方法可以做到这一点?

您可以尝试使用
Q
对象:

from django.db.models import Q
Blog.objects.filter(Q(key__startswith='a') | Q(key__startswith='b'))

或者使用
regex
iregex

Blog.objects.filter(key__iregex=r'^(a|b)\.')

您可以尝试使用
Q
对象:

from django.db.models import Q
Blog.objects.filter(Q(key__startswith='a') | Q(key__startswith='b'))

或者使用
regex
iregex

Blog.objects.filter(key__iregex=r'^(a|b)\.')

您可以尝试使用
Q
对象:

from django.db.models import Q
Blog.objects.filter(Q(key__startswith='a') | Q(key__startswith='b'))

或者使用
regex
iregex

Blog.objects.filter(key__iregex=r'^(a|b)\.')

您可以尝试使用
Q
对象:

from django.db.models import Q
Blog.objects.filter(Q(key__startswith='a') | Q(key__startswith='b'))

或者使用
regex
iregex

Blog.objects.filter(key__iregex=r'^(a|b)\.')

但是你假设这个列表有两个值。那不能解决我的问题。谢谢,也许来一杯雷格士?官方文档:我喜欢regex解决方案,尽管我不确定关键列表增长会带来什么后果。如果一天内没有其他解决方案,我会将此标记为已解决。谢谢。但是你假设这个列表有两个值。那不能解决我的问题。谢谢,也许来一杯雷格士?官方文档:我喜欢regex解决方案,尽管我不确定关键列表增长会带来什么后果。如果一天内没有其他解决方案,我会将此标记为已解决。谢谢。但是你假设这个列表有两个值。那不能解决我的问题。谢谢,也许来一杯雷格士?官方文档:我喜欢regex解决方案,尽管我不确定关键列表增长会带来什么后果。如果一天内没有其他解决方案,我会将此标记为已解决。谢谢。但是你假设这个列表有两个值。那不能解决我的问题。谢谢,也许来一杯雷格士?官方文档:我喜欢regex解决方案,尽管我不确定关键列表增长会带来什么后果。如果一天内没有其他解决方案,我会将此标记为已解决。谢谢