python - How do I return 404 when tastypie is interfacing non-ORM sources? -


i using facade-like pattern described here: http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html

def obj_get(self, request=none, **kwargs):     rv = myobject(init=kwargs['pk'])     audit_trail.message( ... )     return rv 

i can't return none, tosses error.

you should raise exception: tastypie.exceptions.notfound (according code documentation).

i working on tastypie couchdb , delved issue. in tastypie.resources.resource class can find method have override:

def obj_get(self, request=none, **kwargs):     """     fetches individual object on resource.      needs implemented @ user level. if object can not     found, should raise ``notfound`` exception.      ``modelresource`` includes full working version specific django's     ``models``.     """     raise notimplementederror() 

my example:

def obj_get(self, request=none, **kwargs):     """     fetches individual object on resource.      needs implemented @ user level. if object can not     found, should raise ``notfound`` exception.     """     id_ = kwargs['pk']     ups = upsdao().get_ups(ups_id = id_)     if ups none:         raise notfound(                 "couldn't find instance of '%s' matched id='%s'."%                 ("upsresource", id_))     return ups 

one thing strange me. when had @ obj_get method in modelresource class (the superclass of resource class):

def obj_get(self, request=none, **kwargs):     """     orm-specific implementation of ``obj_get``.      takes optional ``kwargs``, used narrow query find     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) k, v in kwargs.items()])          if len(object_list) <= 0:             raise self._meta.object_class.doesnotexist("couldn't find instance of '%s' matched '%s'." % (self._meta.object_class.__name__, stringified_kwargs))         elif len(object_list) > 1:             raise multipleobjectsreturned("more '%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).") 

an exception: self._meta.object_class.doesnotexist raised when object not found, becomes objectdoesnotexist exception - not consensus inside project.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -