tastypie身份验证/自定义响应

时间:2011-09-15 00:50:44

标签: python django rest tastypie

是否有任何在tastypie中使用API​​Key身份验证的示例。提供的示例并未详细说明其工作原理。

此外,我正在查看食谱,试图找到哪个钩子可以在帖子上返回自定义消息。

例如,有人发帖试图创建用户,但用户已经存在。在我的hydrate_user(self,bundle)中:我相信我会做检查但是如何返回有用的错误消息?

1 个答案:

答案 0 :(得分:3)

你可以这样做:

from tastypie.exceptions import ImmediateHttpResponse
from tastypie.http import HttpBadRequest

if test_fails:
    raise ImmediateHttpResponse(HttpBadRequest("User already exists"))