2012-11-12 63 views
3

我正嘗試使用tastypie與非orm使用redis。django tastypie與非orm使用redis不返回對象

我實現了一個自定義的資源,像http://django-tastypie.readthedocs.org/en/v0.9.11/non_orm_data_sources.html

建議下面是部分代碼:

class OrderResource(Resource): 
    order_id = fields.CharField(attribute='order_id') 
    store_url = fields.CharField(attribute='store_url') 
    products = fields.ListField(attribute='products') 

    class Meta: 
     queryset = Order 
     resource_name = 'order' 
     allowed_methods = ['get', 'post', 'put', 'delete', 'patch'] 
     authorization = Authorization() 

    def _client(self): 
     return redis.Redis('localhost') 

    def detail_uri_kwargs(self, bundle_or_obj): 
     kwargs = {} 
     if isinstance(bundle_or_obj, Bundle): 
      kwargs['pk'] = bundle_or_obj.obj.order_id 
     else: 
      kwargs['pk'] = bundle_or_obj.order_id 

     return kwargs 

    def get_object_list(self, request): 
     query = self._client() 
     results = list() 

     for store_url in query.smembers('store_url'): 
      orders_id = query.hgetall('store_url:%s' % store_url) 
      for order in orders_id.keys(): 
       order = Order(store_url=store_url, order_id=order) 
       results.append(order) 

     return results 

    def obj_get_list(self, request=None, **kwargs): 
     # Filtering disabled for brevity... 
     return self.get_object_list(request) 

但是當我嘗試檢索所有訂單,JSON objects是空的,即使有total_count沒錯。 我檢查和包是正確的:

<Bundle for obj: '<orders.models.Order object at 0x10c6f1e90>' and with data: '{'order_id': u'1', 'store_url': u'test.com', 'products': [u'a', u'b', u'c'], 'resource_uri': None}'> 

我在做什麼錯?

OBS:我不能使用Django的nonrel

回答

2

什麼是Order在你的代碼?用Resource你不需要Meta.queryset。嘗試刪除它。

另外,請確保Order是可序列化的。很可能Tastypie無法序列化它。先嚐試使用簡單的字典對象,而不是先訂購。詳細瞭解SerializationDehydration

+0

這也有道理,但wasnt問題。問題是我忘記在Meta上設置'object_class' – silviomoreto