2

当我向 Tastypie API 发出 POST 请求时出现以下错误:

{"error_message": "", "traceback": "Traceback (most recent call last):\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py\", line 195, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py\", line 407, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py\", line 436, in dispatch\n response = method(request, **kwargs)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py\", line 1194, in post_list\n updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs))\n\n File \"/home/kelp/webapps/goals/goals/main/api.py\", line 191, in obj_create\n bundle = super(JoinedGoalResource, self).obj_create(bundle, request, user=request.user)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py\", line 1839, in obj_create\n bundle = self.full_hydrate(bundle)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/resources.py\", line 716, in full_hydrate\n value = field_object.hydrate(bundle)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/fields.py\", line 643, in hydrate\n value = super(ToOneField, self).hydrate(bundle)\n\n File \"/home/kelp/lib/python2.7/django_tastypie-0.9.11-py2.7.egg/tastypie/fields.py\", line 152, in hydrate\n elif self.attribute and getattr(bundle.obj, self.attribute, None):\n\n File \"/home/kelp/webapps/goals/lib/python2.7/django/db/models/fields/related.py\", line 301, in __get__\n raise self.field.rel.to.DoesNotExist\n\nDoesNotExist\n"}

我如何理解这个错误?

4

1 回答 1

3

理解 django 回溯的最好方法是从底部开始向上移动,直到你得到一些属于你的代码。通常 django 非常可靠,通常问题出在您正在做的事情上:

文件 \"/home/kelp/webapps/goals/goals/main/api.py\",第 191 行,在 obj_create\n bundle = super(JoinedGoalResource, self).obj_create(bundle, request, user=request.user)

很可能您正在传递对 create 的引用,并且它试图解决坏对象上的外键关系。

我无法从您发布的内容中看到它,但是您可以查看回溯中的最后一个条目,并查看它正在查看的对象以及它正在访问的不存在的关系。提示是它与您的捆绑对象和错误字段有关。

于 2012-08-05T06:07:06.917 回答