gpt4 book ai didi

python - tastypie get_list 过滤器查询集

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:57 25 4
gpt4 key购买 nike

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)

这是我的 Tastypie 资源。如果我从 auth.ValidateUser() 找到用户,我只需要回复项目表中的关联项目。如果没有,我想显示一个错误字典,指出没有分配项目。我无法向 get_list 添加过滤器。我可以使用 get_object_list 但如果我想回复一个自定义响应,例如错误 dict ({'error': '没有用户映射。'}) 它会抛出错误 AttributeError: "'dict' object has no属性“过滤器””

非常感谢您的帮助。

最佳答案

您可以在 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)

关于python - tastypie get_list 过滤器查询集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34989025/

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