2012-06-05 7 views
7

Używam wzorzec fasady, jak opisany tutaj: http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.htmlJak mogę zwrócić 404, gdy tastypie łączy się ze źródłami innymi niż ORM?

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

nie mogę wrócić Zadne rzuca błąd.

+1

Badając źródło sugeruje podniesienie NOTFOUND Exception (od tastypie.exception) z istoty ciała: { "ERROR_MESSAGE": „Niestety, wniosek ten mógł nie przetwarzać. Spróbuj ponownie później. "} –

Odpowiedz

6

Powinieneś zgłosić wyjątek: tastypie.exceptions.NotFound (zgodnie z dokumentacją kodu).

Pracuję nad tastypie dla CouchDB i zagłębiłem się w problem. W klasie tastypie.resources.Resource można znaleźć metody, które trzeba zastąpić:

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() 

moim przykładzie:

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 

Jedno jest dziwne dla mnie. Gdy miała wygląd w sposób obj_get w klasie ModelResource (nadklasą klasy materiałów):

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).") 

Wyjątek self._meta.object_class.DoesNotExist podnosi się, gdy obiekt nie znajduje się, co w końcu staje ObjectDoesNotExist wyjątkiem - więc nie jest to zgoda wewnątrz projektu.

+0

Źródło wyjątków https://github.com/toastdriven/django-tastypie/blob/master/tastypie/exceptions.py – TankorSmash

2

używam ObjectDoesNotExist exeption z django.core.exceptions

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 
Powiązane problemy