Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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 - Fatal编程技术网

Python 基于共享列django组合查询集

Python 基于共享列django组合查询集,python,django,Python,Django,型号.py class sells(models.Model): #unimportant stuff here type = ... supplier = ... user = ... item_id = models.AutoField(primary_key=True) class sale_items(models.Model): item_id = models.OnetoOneField(sells, on_delete=models

型号.py

class sells(models.Model):
    #unimportant stuff here
    type = ...
    supplier = ...
    user = ...
    item_id = models.AutoField(primary_key=True)

class sale_items(models.Model):
    item_id = models.OnetoOneField(sells, on_delete=models.CASCADE, null=True)
    item_name = ...
    description = ... 
    #other unimportant stuff
def SearchView(request):
    query_results = sale_items.objects.filter(Q(item_name__icontains=search)
                      Q(description__icontains=search))
    return render(request, 'db/search_results.html', {'query_results':query_results})
所以我正在尝试实现搜索,现在我有了它,搜索可以返回在sale_项目中找到的任何信息,比如名称和描述。但是我还需要输出一个项目类型和供应商,它位于销售模型中。基本上,我是在尝试这样做:

SELECT A.item_id, A.item_name ... B.type, B.supplier ... 
FROM sale_items A, sells B
WHERE A.item_id = B.item_id
视图.py

class sells(models.Model):
    #unimportant stuff here
    type = ...
    supplier = ...
    user = ...
    item_id = models.AutoField(primary_key=True)

class sale_items(models.Model):
    item_id = models.OnetoOneField(sells, on_delete=models.CASCADE, null=True)
    item_name = ...
    description = ... 
    #other unimportant stuff
def SearchView(request):
    query_results = sale_items.objects.filter(Q(item_name__icontains=search)
                      Q(description__icontains=search))
    return render(request, 'db/search_results.html', {'query_results':query_results})
我没有包含变量搜索的代码,但它是输入到搜索栏中的文本。查询结果是正确的,在我的模板(search\u results.html)中,我使用for循环打印项目名称和描述

我还想在销售模型中包含与同一商品id相对应的信息。我一直在尝试很多不同的方法来实现这一点,但无法理解

试试这个:

query_results = sale_items.objects.filter(Q(...) | Q(sells__type__icontains=search))

类型和供应商/用户与搜索无关。搜索将根据名称和描述返回项目。每个项目都有一个id,该id位于“销售”和“销售”项目中。所以我需要使用该id从两个模型中获取相应的信息。