Python Django';非类型';对象没有属性';obj';
我知道有几篇文章是关于同样的问题的,但是解决方法是在代码中出现一些我无法理解的错误。所以我在这里发布我到目前为止写的东西,希望能得到你们的帮助。 我有一个类节点,当我执行一篇文章时,我得到了标题中声明的错误。 这是我的代码:Python Django';非类型';对象没有属性';obj';,python,django,tastypie,Python,Django,Tastypie,我知道有几篇文章是关于同样的问题的,但是解决方法是在代码中出现一些我无法理解的错误。所以我在这里发布我到目前为止写的东西,希望能得到你们的帮助。 我有一个类节点,当我执行一篇文章时,我得到了标题中声明的错误。 这是我的代码: class NodeResource(ModelResource): class Meta: queryset = api.models.Node.objects.all() resource_name = _Helpers.node_resource_n
class NodeResource(ModelResource):
class Meta:
queryset = api.models.Node.objects.all()
resource_name = _Helpers.node_resource_name
always_return_data = True
# Allow retrieving large quantities of nodes at once.
limit = 250
max_limit = 0
filtering = {'name', 'is_ulg', 'latitude', 'longitude'}
allowed_methods = ['get', 'post']
authentication = Authentication()
authorization = Authorization()
def obj_create(self, bundle, **kwargs):
node = api.models.Node(name=bundle.data['name'],
is_ulg=bundle.data['is_ulg'],
latitude=bundle.data.get("latitude"),
longitude=bundle.data.get("longitude"))
node.save()
模型如下所示:
class Node(models.Model):
"""
Represents a node in the graph.
"""
name = models.CharField(max_length=255)
is_ulg = models.BooleanField(default=False, verbose_name='Is this node a member of the ULg?')
latitude = models.FloatField()
longitude = models.FloatField()
def __str__(self):
return self.name
class Meta:
ordering = ['name']
unique_together = ("latitude", "longitude")
{"error_message":"'NoneType' object has no attribute 'obj'","traceback":"Traceback (most recent call last):\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 202, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 433, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 465, in dispatch\n response = method(request, **kwargs)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 1347, in post_list\n updated_bundle = self.full_dehydrate(updated_bundle)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 853, in full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle, for_list=for_list)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/fields.py\", line 116, in dehydrate\n current_object = bundle.obj\n\nAttributeError: 'NoneType' object has no attribute 'obj'\n"}
当我使用以下json执行post时
{"name":"Node name","latitude": "2.4567", "longitude":"2.345", "is_ulg":false}
节点的创建是正确的,但我总是得到标题中所述的错误。完全错误如下所示:
class Node(models.Model):
"""
Represents a node in the graph.
"""
name = models.CharField(max_length=255)
is_ulg = models.BooleanField(default=False, verbose_name='Is this node a member of the ULg?')
latitude = models.FloatField()
longitude = models.FloatField()
def __str__(self):
return self.name
class Meta:
ordering = ['name']
unique_together = ("latitude", "longitude")
{"error_message":"'NoneType' object has no attribute 'obj'","traceback":"Traceback (most recent call last):\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 202, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 433, in dispatch_list\n return self.dispatch('list', request, **kwargs)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 465, in dispatch\n response = method(request, **kwargs)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 1347, in post_list\n updated_bundle = self.full_dehydrate(updated_bundle)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/resources.py\", line 853, in full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle, for_list=for_list)\n\n File \"\/usr\/lib\/python2.7\/site-packages\/tastypie\/fields.py\", line 116, in dehydrate\n current_object = bundle.obj\n\nAttributeError: 'NoneType' object has no attribute 'obj'\n"}
知道我做错了什么吗?
谢谢 您的
对象\u create
函数隐式返回None
,但Tastypie希望它返回一个bundle。查看如何在中实现它
但是,由于您似乎没有使用非ORM数据,您可以跳过
obj_create
,让Tastypie为您创建资源。您的对象_create
函数隐式返回None
,但Tastypie希望它返回一个包。查看如何在中实现它
但是,由于您似乎没有使用非ORM数据,您可以跳过
obj_create
,让Tastypie为您创建资源。我永远都不会感谢@Railside!我欠你:)你有个意大利朋友!我永远都不会对Railslide心存感激!我欠你:)你有个意大利朋友!