【问题标题】:Filtering Objects Based On Another Model基于另一个模型过滤对象
【发布时间】:2021-05-31 05:47:17
【问题描述】:

我有一个网站,服务提供商可以将他们的服务上传到该网站,除了他们的服务之外,他们还可以上传他们创建的每项服务的常见问题解答。

所以我有两个单独的模型,每个模型都用外键链接。

下面是我的model.py 文件,里面有这两个模型

class Services(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField(editable=False, max_length=130)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    intro = models.CharField(max_length=100, unique=True)
    pricing = models.IntegerField()
    detail = SummernoteTextField()
    image = models.ImageField(default='default.png', upload_to='services')
    author  = models.ForeignKey(User, on_delete=models.CASCADE, null=True)


class FAQ(models.Model):
    question = models.CharField(max_length=150, help_text="The possible question that a client could ask you")
    answer = models.TextField(help_text="The response to the question above")
    service = models.ForeignKey(Services, on_delete=models.CASCADE, related_name='faq_service', help_text="Which service is this FAQ linked to?")
    author = models.ForeignKey(User, on_delete=models.CASCADE)

在我的views.py 中,我添加了允许我在包含帖子所有详细信息的同一个 DetailView 中显示常见问题解答的 ListView 的功能。 我的views.py 看起来像这样:

class ServiceDetailView(DetailView):
    model = Services

    def get_context_data(self, *args, **kwargs):
        context = super(ServiceDetailView, self).get_context_data(*args, **kwargs)
        context['faq'] = FAQ.objects.all() # to fix-->Need to only show FAQ of that service 
        return context 

现在,我需要能够根据仅列出的服务的常见问题解答过滤常见问题解答部分。我不知道该怎么做。我尝试访问文档,但没有针对此类场景的解决方案,并且我不想将常见问题解答添加到服务模型中,因为我需要服务提供商能够列出他们想要的尽可能多的常见问题解答。

如果有人想知道我的模板显示的样子,这里就是

<h3 class="mt-4 mb-4">Service FAQ</h3>

<div class="accordion accordion-flush" id="accordionFlushExample">
  {% for faq in faq %}
  <div class="accordion-item">
    <h2 class="accordion-header" id="flush-headingOne">
      <button
        class="accordion-button collapsed"
        type="button"
        data-mdb-toggle="collapse"
        data-mdb-target="#flush-collapseOne{{ faq.id }}"
        aria-expanded="false"
        aria-controls="flush-collapseOne"
      >
        {{ faq.question }}
      </button>
    </h2>
    <div
      id="flush-collapseOne{{ faq.id }}"
      class="accordion-collapse collapse"
      aria-labelledby="flush-headingOne"
      data-mdb-parent="#accordionFlushExample"
    >
      <div class="accordion-body">
        {{ faq.answer }}
      </div>
    </div>
  </div> 
  {% endfor %}
</div>

【问题讨论】:

    标签: django django-models filter django-views


    【解决方案1】:

    您过滤service 字段,使其等于self.object。在大多数从SingleObjectMixin [Django-doc] 继承的视图中,第一步是将object 属性附加到视图对象:

    class ServiceDetailView(DetailView):
        model = Services
    
        def get_context_data(self, *args, **kwargs):
            context = super().get_context_data(*args, **kwargs)
            context['faq'] = FAQ.objects.filter(
                service=self.object
            )
            return context

    注意:通常 Django 模型被赋予一个单数名称,所以Service而不是Services

    【讨论】:

    • 非常感谢。我想我真的需要重新阅读文档并了解self。它以我想要的方式工作。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 2021-01-26
    • 1970-01-01
    • 2019-10-03
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多