简体   繁体   English

tastypie get_list过滤器queryset

[英]tastypie get_list filter queryset

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)

This is my tastypie resource. 这是我的美味资源。 If I found a user from auth.ValidateUser() I need to reply only the associate project from projects table. 如果我从auth.ValidateUser()中找到了一个用户,则只需要从projects表中答复关联的项目。 If not I want to display a error dict saying no project is assigned. 如果没有,我想显示一个错误命令,说没有分配项目。 I'm not a able to add a filter to get_list. 我无法将过滤器添加到get_list。 I'm able to do with get_object_list but if I wanted to reply a custom response like error dict ({'error': 'No user mapped.'}) it is throwing error AttributeError: "'dict' object has no attribute 'filter'" 我可以使用get_object_list进行操作,但是如果我想回复自定义响应,例如error dict({'error':'No user map。'}),则会抛出错误AttributeError: "'dict' object has no attribute 'filter'"

Any help is much appreciated here. 任何帮助在这里非常感谢。

You could do the filtering in get_object_list , then in get_list if the queryset is empty return your dict. 您可以在get_object_list进行过滤,然后如果queryset为空,则在get_list进行过滤,返回您的字典。

Alternatively, in get_object_list you can do: 或者,您可以在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM