字段不允许过滤(不使用关系)

时间:2012-09-05 15:56:36

标签: django tastypie

我的django和tastypie有问题

给出以下代码:

class CandidatePollResource(ModelResource):
    class Meta:
        queryset = Candidate.objects.all()
        resource_name = "candidate-poll"
        filtering = {"status": ALL }

class Candidate(Profile):
    """
    This profile stores all information about a candidate.
    """
    status = models.CharField(_('status'), max_length=15, blank=True, choices=CANDIDATE_STATUS_CHOICES)

class Profile(models.Model):
    """
    Abstract basic class all profiles should inherit.
    """
    user = models.OneToOneField(CustomUser,related_name='%(class)s',)
    invitationdate = models.DateField(_('invitationdate'), null=True, blank=True)
    confirmationdate = models.DateField(_('confirmationdate'), null=True, blank=True)
    activation_key = models.CharField(_('activation_key'), max_length=32, blank=True)
    # Adding of "class" here is important for this to work. See
    # http://thedjangoforum.com/board/thread/417/reverse-query-name-for-field-clash/?page=1
    created_by = models.ForeignKey(CustomUser, related_name='%(class)s_created', blank=True, null=True)

    objects = ProfileManager()

    class Meta:
        abstract = True

每次我们尝试调用过滤结果集(http://localhost:3000/api/v1/candidate-poll/?status__exact=new)时,我总是会收到以下错误

The 'status' field does not allow filtering.

如何在该字段上启用过滤?

2 个答案:

答案 0 :(得分:4)

我认为你的语法不完全正确。 而不是:

filtering = {"status": ("exact", "in",), }

尝试:

filtering = {"status": [ "exact", "in" ] }

如果这不起作用,你可以试试:

filtering = {"status": ALL }

然后从那里开始。 ALL应该允许一切,如果它不起作用,这将意味着问题出在其他地方。

有关详细信息,请查看Tastypie docs

答案 1 :(得分:0)

过滤= {'状态':['精确']}

现在您可以在URL http://localhost:3000/api/v1/candidate-poll/?status=new中使用该字段,并且仅使用状态为new的json数据过滤该字段