2013-02-12 88 views
3

我试图一次性创建资源和相关资源,但出现错误。我究竟做错了什么?Django和Tastypie:如何一次性发布新资源并创建相关资源?

这是我的代码和我所想POST:

模式

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() 

如何挑起错误

从卷曲 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 --dump-header - --header 'Content-Type: application/xml' -X POST -d @post_asset.xml 'http://localhost:8000/ondemandbackend/api/v1/asset/' 

内容

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 
} 
+0

你有404服务器错误,这意味着你有问题的帖子url或你的资源没有注册到api。 – UnLiMiTeD 2013-02-12 12:32:13

+0

你有一个'ToOneField'给你的AssetResource,你没有在你的POST请求中填充,在TastyPie中它总是查找相关的字段,而不管它是否获得POST中的属性,因此在尝试查找非人口领域。我不确定如何解决这个问题。 – mrmagooey 2013-02-18 23:35:40

回答

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字段,但对我来说,似乎并不像你这样做。

当你省略那部分时,我得到了和你一样的错误。