Python 当tastypie与非ORM源接口时,如何返回404?
我使用的是这里描述的外观样式:Python 当tastypie与非ORM源接口时,如何返回404?,python,rest,http-status-code-404,tastypie,Python,Rest,Http Status Code 404,Tastypie,我使用的是这里描述的外观样式: 我无法返回None,抛出一个错误。您应该引发一个异常:tastype.exceptions.NotFound(根据代码文档) 我正在为CouchDB开发tastypie并深入研究这个问题。在tastype.resources.Resource类中,可以找到必须重写的方法: def obj_get(self, request=None, **kwargs): """ Fetches an individual object on the resour
我无法返回None,抛出一个错误。您应该引发一个异常:tastype.exceptions.NotFound(根据代码文档) 我正在为CouchDB开发tastypie并深入研究这个问题。在tastype.resources.Resource类中,可以找到必须重写的方法:
def obj_get(self, request=None, **kwargs):
"""
Fetches an individual object on the resource.
This needs to be implemented at the user level. If the object can not
be found, this should raise a ``NotFound`` exception.
``ModelResource`` includes a full working version specific to Django's
``Models``.
"""
raise NotImplementedError()
我的例子是:
def obj_get(self, request=None, **kwargs):
"""
Fetches an individual object on the resource.
This needs to be implemented at the user level. If the object can not
be found, this should raise a ``NotFound`` exception.
"""
id_ = kwargs['pk']
ups = UpsDAO().get_ups(ups_id = id_)
if ups is None:
raise NotFound(
"Couldn't find an instance of '%s' which matched id='%s'."%
("UpsResource", id_))
return ups
有一件事对我来说很奇怪。当我查看ModelResource类(资源类的超类)中的obj_get方法时:
def obj_get(self,request=None,**kwargs):
"""
特定于ORM的“obj_get”实现。
获取可选的``kwargs``,用于缩小要查找的查询范围
实例。
"""
尝试:
基本对象列表=自。获取对象列表(请求)。过滤器(**kwargs)
对象\u列表=自身。应用\u授权\u限制(请求、基本\u对象\u列表)
stringified_kwargs=','。连接([%s=%s”%(k,v)表示kwargs.items()中的k,v)
如果len(对象列表)1:
引发多个对象返回(“超过“%s”个匹配“%s”。%”(self.\u meta.object\u class.\u name\u,stringized\u kwargs))
返回对象列表[0]
除值错误外:
raise NotFound(“提供的资源查找数据无效(类型不匹配)。”
一个异常:self.\u meta.object\u class.DoesNotExist在找不到对象时引发,最终成为ObjectDoesNotExist异常-因此它在项目内部不是一致的 我使用来自django.core.exceptions的ObjectDoesNotExist验证
def obj_get(self, request=None, **kwargs):
try:
info = Info.get(kwargs['pk'])
except ResourceNotFound:
raise ObjectDoesNotExist('Sorry, no results on that page.')
return info
检查源建议引发NotFound异常(来自tastype.Exception),主体为:{“error_message”:“抱歉,无法处理此请求。请稍后重试。”}以及异常源
def obj_get(self, request=None, **kwargs):
"""
A ORM-specific implementation of ``obj_get``.
Takes optional ``kwargs``, which are used to narrow the query to find
the instance.
"""
try:
base_object_list = self.get_object_list(request).filter(**kwargs)
object_list = self.apply_authorization_limits(request, base_object_list)
stringified_kwargs = ', '.join(["%s=%s" % (k, v) for k, v in kwargs.items()])
if len(object_list) <= 0:
raise self._meta.object_class.DoesNotExist("Couldn't find an instance of '%s' which matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))
elif len(object_list) > 1:
raise MultipleObjectsReturned("More than '%s' matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))
return object_list[0]
except ValueError:
raise NotFound("Invalid resource lookup data provided (mismatched type).")
def obj_get(self, request=None, **kwargs):
try:
info = Info.get(kwargs['pk'])
except ResourceNotFound:
raise ObjectDoesNotExist('Sorry, no results on that page.')
return info