我有关于django tastypie的问题,我想将数据发布到api, 但它返回400,错误消息为空 我的资源是非orm,所以我重写obj_create方法 但GET方法效果很好。 这是我的代码
class DitcObject(object):
def __init__(self, initial=None):
self.__dict__['_data'] = {}
if hasattr(initial, 'items'):
self.__dict__['_data'] = initial
def __getattr__(self, item):
return self._data.get(item, None)
def __setattr__(self, key, value):
self.__dict__['_data'][key] = value
def to_dict(self):
return self._data
class QueueResource(Resource):
qid = fields.CharField(attribute='qid')
queue = fields.CharField(attribute='queue')
class Meta:
resource_name = 'queue'
object_class = DitcObject
authorization = Authorization()
def _client(self):
return RedisTool().get_redis()
def detail_uri_kwargs(self, bundle_or_obj):
kwargs = {}
if isinstance(bundle_or_obj, Bundle):
kwargs['pk'] = bundle_or_obj.obj.qid
else:
kwargs['pk'] = bundle_or_obj.qid
return kwargs
def get_object_list(self, request):
queues = filter(lambda item:re.match(r'%s_update_plan\..+\..+' % PRO_FLAG, item), self._client().keys())
results = []
for result in queues:
new_obj = DitcObject(initial={'queue': list(self._client().smembers(result))})
new_obj.qid = result
results.append(new_obj)
return results
def obj_get_list(self, bundle, **kwargs):
qid = bundle.request.GET.get('qid')
if qid:
return [obj for obj in self.get_object_list(bundle.request) if obj.qid == qid]
return self.get_object_list(bundle.request)
def obj_get(self, bundle, **kwargs):
queue = self._client().smembers(kwargs['pk'])
return DitcObject(initial={'queue': list(queue), 'qid': kwargs['pk']})
def obj_create(self, bundle, **kwargs):
bundle = self.full_hydrate(bundle)
print kwargs
return bundle
这是我的错误回复
HTTP/1.0 400 BAD REQUEST
Date: Tue, 14 Jan 2014 09:20:35 GMT
Server: WSGIServer/0.1 Python/2.7.3
Content-Type: application/json
{"error": ""}
谢谢大家!
答案 0 :(得分:2)
您很有可能{J}数据POST
,并且您忘记用双引号"
包围JSON数据的键
在开始使用TastyPie时,我遇到了一件事。