例外を発生させる必要があります: Tastypie.exceptions.NotFound (コード ドキュメントによると)。
私はCouchDBのtastypieに取り組んでおり、問題を掘り下げました。Tastypie.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
私にとって奇妙なことが1つあります。ModelResource クラス (Resource クラスのスーパークラス) の obj_get メソッドを見たとき:
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).")
例外: self._meta.object_class.DoesNotExist は、オブジェクトが見つからない場合に発生します。これは最終的に ObjectDoesNotExist 例外になります。そのため、プロジェクト内でコンセンサスが得られません。