django 多字段模糊查询
(2014-05-04 16:49:48)
2014年5月4日16:51:18
def get(self,
request,page, *args, **kwargs):
sqlstr=request.session["VipSearachTxt"]
carOwner=
CarOwner.objects.filter(Q(Name__contains=sqlstr)|Q(MUser__username__contains=sqlstr)|Q(VIPId__VIPMachineCode__contains=sqlstr)|Q(VipState__StateName__contains=sqlstr))
#sql Q()相当于where or 查询,查询外键的话,字段__外键的其他字段
page_obj = JuncheePaginator(carOwner,8)
try:
thePage =
page_obj.page(int(page))
except PageNotAnInteger:
thePage =
page_obj.page(int(1))
except EmptyPage:
thePage =
page_obj.page(page_obj.num_pages)
return
render_to_response('OMS_VipSerachList.html',{'page_obj':thePage,
'is_paginated': page_obj.num_pages >
1,},context_instance=RequestContext(request))
1.django 用Q(a)|Q(b)来实现 sql中 where a or b 功能
2.Q(Name__contains=sqlstr) 这句的意思是 在 sql 中 like
'%sqlstr%'
3.Q(VIPId__VIPMachineCode__contains=sqlstr) 意思跟上面一样,不过要注意 VIPId 在CarOwner中是一个外键,引用的字段是VIP中的主键
4.如果filter()函数中有逗号,是代表WHERE AND 的意思
具体实现代码
class VipSerachList(ListView):