我正在尝试根据空格" "
拆分查询。例如first_name
last_name
,但是如果我尝试检查我的座位中的空间,则会收到错误消息
“ NoneType”类型的参数不可迭代
if ' ' in query: #This is where is the error is generated
尝试解决上述问题。下面是完整的代码
class QList(SelectRelatedMixin, ListView):
model = Question
def get_queryset(self):
queryset = super(QList, self).get_queryset().order_by('-created_at')
query = self.request.GET.get('q')
if ' ' in query:
query = query.split()
queryset = queryset.filter(
chain(User.objects.filter(first_name__icontains=query[0], last_name__icontains=query[1]),
User.objects.filter(first_name__icontains=query[1], last_name__icontains=query[0])))
return queryset
else:
queryset = queryset.filter(
Q(user__username__iexact=query) |
Q(user__first_name__iexact=query) |
Q(user__last_name__iexact=query)
)
return queryset
任何人都可以建议解决方法
答案 0 :(得分:1)
要拆分字符串,只需说出您的名字是“ Marco Bianchi”即可。
query = "Marco Bianchi"
first_name = query.split(" ")[0]
last_name = query.split(" ")[1]
更新: 代替
if ' ' in query:
query = query.split()
使用此
query = query.split(" ")
答案 1 :(得分:0)
您可以使用分割字符串,例如:
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;