【问题标题】:Can you include a TaggableManager as a filterset for django-filter?你可以包含一个 TaggableManager 作为 django-filter 的过滤器集吗?
【发布时间】:2013-12-13 14:17:13
【问题描述】:

我在我的 Web 应用程序中同时使用 django-taggit 和 django-filter,它存储法律决定。我的主要观点(下图)继承自股票django-filter FilterView,并允许人们通过法规和法规的一部分过滤决策。

class DecisionListView(FilterView):
    context_object_name = "decision_list"
    filterset_class = DecisionFilter
    queryset = Decision.objects.select_related().all()

    def get_context_data(self, **kwargs):
        # Call the base implementation to get a context
        context = super(DecisionListView, self).get_context_data(**kwargs)
        # Add in querysets for all the statutes
        context['statutes'] = Statute.objects.select_related().all()
        context['tags'] = Decision.tags.most_common().distinct()
        return context

我还会在添加决策时按主题标记决策,我希望人们也能够对此进行过滤。我目前在models.py 中有以下内容:

class Decision(models.Model):
    citation = models.CharField(max_length = 100)
    decision_making_body = models.ForeignKey(DecisionMakingBody)
    statute = models.ForeignKey(Statute)
    paragraph = models.ForeignKey(Paragraph)
    ...
    tags = TaggableManager()


class DecisionFilter(django_filters.FilterSet):
    class Meta:
        model = Decision
        fields = ['statute', 'paragraph']

我尝试在DecisionFilter 中的fields 列表中添加“标签”,但这没有效果,可能是因为TaggableManager 是一个经理而不是数据库中的一个字段。我没有在文档中找到任何涵盖此内容的应用程序。是否可以对 taggit 标签进行过滤?

【问题讨论】:

    标签: django-filter django-taggit


    【解决方案1】:

    您应该能够使用“tags__name”作为搜索/过滤字段。查看http://django-taggit.readthedocs.org/en/latest/api.html#filtering上的过滤部分

    【讨论】:

      猜你喜欢
      • 2018-04-28
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2011-12-27
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多