我有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.
如何在该字段上启用过滤?
我认为你的语法不完全正确。代替:
filtering = {"status": ("exact", "in",), }
尝试:
filtering = {"status": [ "exact", "in" ] }
如果这不起作用,你可以尝试:
filtering = {"status": ALL }
并从那里开始。 ALL
应该允许一切,如果它不起作用,这将意味着问题在其他地方。
欲了解更多信息,请查看Tastypie docs
跟着去吧
filtering = {'status':['exact']}
现在你可以在URL http://localhost:3000/api/v1/candidate-poll/?status=new中使用,只使用状态为new的json数据过滤字段