简体   繁体   English

比较来自多个查询的Django-taggit标签,并列出匹配标签中的对象

[英]Compare Django-taggit tags from multiple queries and list objects from matching tags

I have a site that sells parts and tools. 我有一个出售零件和工具的网站。 The site was originally built with separate models for tools and documents for those tools. 该站点最初是用单独的工具模型和这些工具的文档构建的。 The documents were just iterated over on a document page and linked to from the tool page. 文档只是在文档页面上迭代,并从工具页面链接到。 We use Django-taggit to tag everything, but I have tagged myself into a corner :) 我们使用Django-taggit标记所有内容,但我已将自己标记在角落:)

I need to list the documents that belong to a particular tool, on the tool detail page and not on separate document page. 我需要在工具详细信息页面而不是单独的文档页面上列出属于特定工具的文档。

The problem is there is no relationship with the documents to the tool, so I am trying to query the tags on the tool and the tags on the documents and any document that has the same tag as the tool, would be listed on the page. 问题是与该工具的文档没有任何关系,因此我试图查询该工具上的标签以及该文档上的标签以及与该工具具有相同标签的任何文档,都将在页面上列出。

Below is what I've come up with so far and it works in the sense that it matches the tags, but it duplicates every document. 以下是我到目前为止提出的内容,它在某种意义上与标签匹配,但是它会复制每个文档。 For example, if I have tag names "one" and "two" on both the tool and the documents, it will list all matching documents multiplied by the number of matched tags. 例如,如果我在工具和文档上都具有标签名“一个”和“两个”,它将列出所有匹配的文档乘以匹配的标签数。 I hope that makes sense... I've also included a screenshot to show an example. 我希望这是有道理的。我还提供了一个截图以显示示例。 I know why my code is duplicating, but my Python chops are not seasoned enough to write the code needed to solve it(I also know this can be written dryer - that's my next challenge). 我知道为什么我的代码是重复的,但是我的Python印章不够熟练,无法编写解决它的代码(我也知道这可以写得更干-这是我的下一个挑战)。 Thank you for your help. 谢谢您的帮助。

在此处输入图片说明

View 视图

def tool_detail(request, tool):
  contact_form(ContactForm, request)
  tags = Tool.tags.all()
  tool = get_object_or_404(Tool, slug=tool)
  uploads = tool.uploads.all()
  doc = Document.objects.all()
  return render(request, 'products/tool_detail.html', {'tool': tool, 'tags': tags, 'doc': doc, 'form': ContactForm, 'uploads': uploads})

Template 模板

...
<tbody>
  {% for d in doc %}
    {% for tag in tool.tags.all %}
        {% if tag.name in d.tags.get.name %}
           <tr>
             <td>{{ d.title }}</td>
             <td style="text-align: center;"><a href="{{ d.file.url }}"><i class="fa fa-cloud-download"></i></a></td>
           </tr>
       {% endif %}
    {% endfor %}
 {% endfor %}
</tbody>
...

First of all, it's a bad practice placing your business logic in template - it should be done in view. 首先,将业务逻辑放在模板中是一种不好的做法-应该在视图中完成。 Having said that, on to your question. 话虽如此,关于您的问题。 First - the view (see comments in code): 首先-视图(请参见代码中的注释):

def tool_detail(request, tool):
    contact_form(ContactForm, request)
    tags = Tool.tags.all()
    tool = get_object_or_404(Tool, slug=tool)
    uploads = tool.uploads.all()
    # filter the documents to find the ones that have identical tags,
    # add .distinct() to filter out duplicate documents
    doc = Document.objects.filter(tags__in=tool.tags.all()).distinct()
    return render(request, 'products/tool_detail.html', {'tool': tool, 'tags': tags,  'doc': doc, 'form': ContactForm, 'uploads': uploads})

and the form would simply be: 形式就是:

...
<tbody>
    {% for d in doc %}
        <tr>
            <td>{{ d.title }}</td>
                <td style="text-align: center;"><a href="{{ d.file.url }}"><i class="fa fa-cloud-download"></i></a>
            </td>
        </tr>
    {% endfor %}
</tbody>
...

Read more about filtering and field lookups in the documentation . 文档中阅读有关过滤和字段查找的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM