简体   繁体   English

我可以使用 django 从我的 model 中的多个数据库中搜索吗?

[英]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.

相关问题 Django:如何使用模型类从Django外部与数据库交互? - Django: How can I use my model classes to interact with my database from outside Django? 如何将数据从表单模型获取到Django中的数据库中 - How can I get data from a form model into a database in Django 使用Django模板,如何在JavaScript标头中访问模型中的值? - Using Django Templates, how can I access values from my model in the JavaScript header? 如何通过单击按钮向用户提供将搜索结果添加到 Django 中的数据库 model 的选项? - How can I provide users option to add search results, with click of button, to database model in Django? 如何使用我自己的 model 容器为集成建模构建多 model 端点? - How can I build a multi model endpoint for ensemble modeling with using my own model containers? 如何从Django数据库中获取固定输出 - How can I get fixed output from my django database 从Django中的模型搜索 - Search from model in Django 如何使用模型中保存的字段搜索 Django 数据库? - How to search a Django database using a field saved in the model? 无法使用内置的身份验证模型和我创建的模型调用/传递来自 django 数据库的数据到网站,这是我无法查询的模型 - Can't call / pass data from django database onto website using built in auth model and a model I created, which is the one I cant query 如何在 Django 中列出从 model 到我的问题 model 的所有答案? 多对一的关系? - How can I list all answers from answer model to my question model in Django ? Relation many to one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM