如何在django-filter

时间:2015-12-21 23:16:55

标签: django django-rest-framework django-filter

使用django-filters,我有判断书写的判断。模型大致如下:

app1:models.py:

from app2.models import Judge
class Judgement(model.Models):
    judges = models.ManyToManyField(Judge, related_name="judgements")
    topic = models.TextField(help_text="The topic the judgement is about."

app2:models.py

class Judge(models.Models):
    name = CharField(max_length=100)
    race = CharField(max_length=5, choices=RACES)

我希望能够通过一个或另一个过滤判断或判断,所以我的过滤器应该是:

class JudgementFilter(filters.FilterSet):
    # THIS DOESN'T WORK. 
    judges = RelatedFilter('JudgeFilter', name='judges')

class JudgeFilter(filters.FilterSet):
    judgements = RelatedFilter(JudgementFilter, name='judgements')

如果有效,我可以向API询问是否根据他们所写判断的属性过滤了法官(例如:"所有法官都对比赛进行了判断")或者I&I #39; d能够过滤由编写它们的法官过滤的判断(例如,"由黑人法官和#34写的所有判断;)。

不幸的是,这不起作用,我收到有关尝试导入字符串值而不是其他内容的错误。有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:0)

我很遗憾以这种方式回答我自己的问题,但this information was in the readme我完全错过了它。

解决方案是使用完整路径:

class JudgementFilter(filters.FilterSet):
    judges = RelatedFilter('path.to.my.app.JudgeFilter', name='judges')

class JudgeFilter(filters.FilterSet):
    judgements = RelatedFilter(JudgementFilter, name='judgements')

然后生活是美好的。