【问题标题】:tastypie get_list filter queryset美味的 get_list 过滤器查询集
【发布时间】:2016-01-25 09:26:49
【问题描述】:
class ProjectResource(ModelResource):
  class Meta(object):
    queryset = models.Projects.objects.all()
    resource_name = 'projects'
    list_allowed_methods = ['get', 'post', 'put']
    always_return_data = True
    default_format = 'application/json'
    user = auth.ValidateUser()

  def get_list(self, request, **kwargs):
    user_projects = super(ProjectResource, self).get_list(request, **kwargs)
    result = json.dumps(user_projects.content)
    return http.HttpResponse(result,
                             content_type='application/json', status=200)

这是我的美味派资源。如果我从 auth.ValidateUser() 找到用户,我只需要从项目表中回复关联项目。如果不是,我想显示一个错误字典,说明没有分配项目。我无法向 get_list 添加过滤器。 我可以使用 get_object_list 但如果我想回复自定义响应,如错误字典 ({'error': 'No user mapped.'}) 它会抛出错误AttributeError: "'dict' object has no attribute 'filter'"

这里非常感谢任何帮助。

【问题讨论】:

  • user = auth.ValidateUser() 应该做什么?

标签: python django tastypie


【解决方案1】:

您可以在get_object_list 中进行过滤,然后在get_list 中,如果查询集为空,则返回您的字典。

或者,您可以在get_object_list 中执行以下操作:

from tastypie.exceptions import ImmediateHttpResponse
response = http.HttpResponse(json.dumps({'error': 'No user mapped.'}),
                         content_type='application/json', status=400)
raise ImmediateHttpResponse(response=response)

【讨论】:

    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2019-07-29
    • 1970-01-01
    • 2011-10-24
    • 2014-04-18
    • 2012-07-11
    相关资源
    最近更新 更多