我正在尝试使用 redis 将美味派与非 orm 一起使用。
我实现了一个自定义资源,如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)
但是当我尝试检索所有订单时,jsonobjects
是空的,即使是total_count
正确的。我检查了一下,Bundle 是正确的:
<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