gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:12:50 24 4
gpt4 key购买 nike

我对 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.
如何在该字段上启用过滤?

最佳答案

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

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

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

如果这不起作用,您可以尝试:
filtering = {"status": ALL }

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

更多信息请查看 Tastypie docs

关于django - 字段不允许过滤(不使用关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12285364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com