【问题标题】:field does not allow filtering (no relation used)字段不允许过滤(不使用关系)
【发布时间】:2012-08-30 09:42:44
【问题描述】:

我对 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.

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

【问题讨论】:

    标签: django tastypie


    【解决方案1】:

    一起去

    过滤 = { 'status' : ['exact'] }

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

    【讨论】:

      【解决方案2】:

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

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

      尝试:

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

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

      filtering = {"status": ALL }
      

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

      更多信息请关注Tastypie docs

      【讨论】:

      • 允许“ALL”过滤完全有帮助:(知道我可以从哪里开始搜索那个错误吗?
      • 这一切看起来都很好,也许可以尝试简化您的字段定义,即代替 status = models.CharField(_('status'), max_length=15, blank=True, choices=CANDIDATE_STATUS_CHOICES) 尝试 status = models.CharField(max_length=15, blank=True) 看看是否有效。如果可以,还请提供Profile 模型的来源,因为它也可能发挥作用。
      • 它没有用。慢慢地我变得绝望。我将配置文件类添加到我的问题中。感谢您的耐心和支持!
      • 不客气 :) 也许你应该尝试用一些超级简单的东西临时替换你当前的 Candidate 模型,即,而不是继承和自定义管理器,只用一个 @987654330 从头开始​​定义一个简单的模型@ 看看 Tastypie 是否允许你过滤它。
      • 嗯,最后是格式/缩进错误,我们的第四位开发人员在她的编辑器中打开它时就出现了。所以这更像是一个愚蠢的错误
      猜你喜欢
      • 2014-07-05
      • 2018-12-12
      • 2011-09-20
      • 1970-01-01
      • 2021-09-10
      • 1970-01-01
      • 2018-12-07
      • 2011-04-29
      • 1970-01-01
      相关资源
      最近更新 更多