Thursday, 15 April 2010

Django and Tastypie: How to POST a new resource and created related resources in one go? -



Django and Tastypie: How to POST a new resource and created related resources in one go? -

i trying create resource , related resources in 1 go, error. doing wrong?

this code , trying post:

models

class="lang-python prettyprint-override">class asset(models.model): basename = models.charfield(max_length=256, unique=true) def __unicode__(self): homecoming 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): homecoming self.directory

the api resources

class="lang-python prettyprint-override">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()

how provoke error

curl --dump-header - --header 'content-type: application/xml' -x post -d @post_asset.xml 'http://localhost:8000/ondemandbackend/api/v1/asset/'

contents of post_asset.xml

class="lang-xml prettyprint-override"><?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>

output 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 phone call last): file "tastypie/resources.py", line 192, in wrapper response = callback(request, *args, **kwargs) file "tastypie/resources.py", line 397, in dispatch_list homecoming 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 homecoming self.resource_from_data(self.fk_resource, value, **kwargs) file "tastypie/fields.py", line 559, in resource_from_data homecoming 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 , getattr(bundle.obj, self.attribute, none): file "django/db/models/fields/related.py", line 343, in __get__ raise self.field.rel.to.doesnotexist }

you have pass in resource_uri described in doc:

curl --dump-header - -h "content-type: application/json" -x post --data '{"body": "this prbbly 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/

that's "user": "/api/v1/user/1/" part. haven't worked xml or m2m fields before me doesn't seem doing that.

i got same error when omitted part.

django tastypie

No comments:

Post a Comment