2

我对 django 和 sweetpie 有疑问

给出以下代码:

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.

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

4

2 回答 2

4

我认为您的语法并不完全正确。代替:

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

尝试:

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

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

filtering = {"status": ALL }

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

有关更多信息,请查看Tastypie 文档

于 2012-09-05T16:54:43.207 回答
0

一起去

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

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

于 2019-04-30T07:13:53.820 回答