3

我正在尝试一次性创建资源和相关资源,但出现错误。我究竟做错了什么?

这是我的代码,也是我要发布的内容:

楷模

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>

卷曲的输出

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
}
4

1 回答 1

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 字段,但对我来说,您似乎没有这样做。

省略该部分时,我遇到了与您相同的错误。

于 2013-03-11T01:58:53.017 回答