Django和Tastypie:如何一次性发布新资源并创建相关资源?

时间:2013-02-12 08:39:43

标签: django tastypie

我正在尝试一次创建资源和相关资源,但是我收到了错误。我做错了什么?

这是我的代码和我想要发布的内容:

模型

class Asset(models.Model):
    basename = models.CharField(max_length=256, unique=True)

    def __unicode__(self):
        return self.basename

class CommonAssetLocation(models.Model):
    asset = models.ForeignKey(Asset, related_name='%(class)s_locations')

    class Meta:
        abstract = True

class LocalDirectory(CommonAssetLocation):
    directory = models.CharField(max_length=256)

    def __unicode__(self):
        return self.directory

API资源

class AssetResource(ModelResource):
    localdirectory_locations = fields.ToManyField(to='ondemandbackend.api.LocalDirectoryResource', attribute='localdirectory_locations', related_name='localdirectory_location', full=True)

    class Meta:
        resource_name = 'asset'
        queryset = Asset.objects.all()
        authorization = Authorization()

class LocalDirectoryResource(ModelResource):
    asset = fields.ToOneField(to='ondemandbackend.api.AssetResource', attribute='asset', related_name='asset')

    class Meta:
        resource_name = 'localdirectory_location'
        queryset = LocalDirectory.objects.all()
        authorization = Authorization()

如何引发错误

curl --dump-header - --header 'Content-Type: application/xml' -X POST -d @post_asset.xml 'http://localhost:8000/ondemandbackend/api/v1/asset/'

post_asset.xml的内容

<?xml version="1.0"?>
<object>
  <basename>post_test</basename>
  <localdirectory_locations type="list">
    <object>
      <directory>/tmp/hello/sup_again3</directory>
    </object>
    <object>
      <directory>/tmp/hello/sup_again2</directory>
    </object>
  </localdirectory_locations>
</object>

Curl的输出

HTTP/1.0 404 NOT FOUND Date: Mon, 11 Feb 2013 13:09:28 GMT Server:
WSGIServer/0.1 Python/2.7.3 Content-Type: application/json;
charset=utf-8

{
"error_message": "", "traceback": "Traceback (most recent call last):
File "tastypie/resources.py", line 192, in wrapper  response = callback(request, *args, **kwargs)
File "tastypie/resources.py", line 397, in dispatch_list  return self.dispatch('list', request, **kwargs)
File "tastypie/resources.py", line 427, in dispatch  response = method(request, **kwargs)
File "tastypie/resources.py", line 1165, in post_list  updated_bundle = self.obj_create(bundle, request=request, **self.remove_api_resource_names(kwargs))
File "tastypie/resources.py", line 1783, in obj_create  m2m_bundle = self.hydrate_m2m(bundle)
File "tastypie/resources.py", line 743, in hydrate_m2m  bundle.data[field_name] = field_object.hydrate_m2m(bundle) 
File "tastypie/fields.py", line 742, in hydrate_m2m  m2m_hydrated.append(self.build_related_resource(value, **kwargs))
File "tastypie/fields.py", line 593, in build_related_resource  return self.resource_from_data(self.fk_resource, value, **kwargs)
File "tastypie/fields.py", line 559, in resource_from_data  return fk_resource.full_hydrate(fk_bundle)
File "tastypie/resources.py", line 698, in full_hydrate  value = field_object.hydrate(bundle)
File "tastypie/fields.py", line 636, in hydrate  value = super(ToOneField, self).hydrate(bundle)
File "tastypie/fields.py", line 154, in hydrate  elif self.attribute and getattr(bundle.obj, self.attribute, None):
File "django/db/models/fields/related.py", line 343, in __get__  raise self.field.rel.to.DoesNotExist
}

1 个答案:

答案 0 :(得分:0)

您必须传递文档中描述的resource_uri

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"body": "This will prbbly be my lst post.", "pub_date": "2011-05-22T00:46:38", "slug": "another-post", "title": "Another Post", "user": "/api/v1/user/1/"}' http://localhost:8000/api/v1/entry/

这是"user": "/api/v1/user/1/"部分。我以前没有使用XML或m2m字段,但对我来说,你似乎并没有这样做。

在省略该部分时,我得到了与你相同的错误。