[英]can i search from multi database in my model using django?
i have this code and work perfect.. but i want search in all my models how to do that?我有这个代码并且工作完美..但我想在我的所有模型中搜索如何做到这一点?
any idea for that?有什么想法吗?
mycode我的代码
Models.py:模型.py:
class OpratingSystems(models.Model):
# def etc ..
class AndroidGames(models.Model):
# def etc ..
class Antivirus(models.Model):
# def etc ..
class AndroidApks(models.Model):
# def etc ..
class PCgames(models.Model):
# def etc ..
class PCprogram(models.Model):
# def etc ..
class Homepage(models.Model):
# def etc ..
Views.py:视图.py:
def search(request):
if request.method == 'GET':
query= request.GET.get('q')
submitbutton= request.GET.get('submit')
if query is not None:
lookups= Q(name__icontains=query) | Q(app_contect__icontains=query) | Q(page_url__icontains=query) | Q(app_image__icontains=query)
results= Homepage.objects.filter(lookups).distinct()
context={'results': results,
'submitbutton': submitbutton}
return render(request, 'html_file/enterface.html', context)
else:
return render(request, 'html_file/enterface.html')
else:
return render(request, 'html_file/enterface.html')
html page: html页面:
{% if submitbutton == 'Search' and request.GET.q != '' %}
{% if results %}
<h1> <small> Results for </small><b>{{ request.GET.q }}</b> : </h1>
<br/><br/>
{% for result in results %}
<label id="label_main_app"> <img id="img_main_app_first_screen" src="{{result.app_image.url}}" alt="no image found !" height="170" width="165" > {{result.name}} <br><br> <p id="p_size_first_page"> {{result.app_contect}} <br> <br> <a href="{{ result.page_url }}" type="button" class="btn btn-primary"><big> See More & Download </big> </a> </p>
</label>
{% endfor %}
{% else %}
<h3> No results for this search </h3>
{% endif %}
{% endif %}
this was my code and work in Homeoage model so how to use all model?这是我的代码并在 Homeoage model 中工作,那么如何使用所有 model?
One solution would be by using the filter() methods and then combine all queries in a single with itertools.chain一种解决方案是使用 filter() 方法,然后将所有查询与 itertools.chain 组合在一起
if query is not None:
query1 =Model1.objects.filter(title__icontains=query)
query2 = Model2.objects.filter(modelfield__icontains=query)
....
#now you can combine all queries with this chain method
from itertools import chain
results= list(chain(query1,query2...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.