Django and Tastypie: how to POST a new resource and create matching resources in one go?

I am trying to create resource and linked resources in one go, but I am getting an error. What am I doing wrong?

This is my code and what I am trying to POST:

Models

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 resources

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 to raise the error

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

      

Post_asset.xml content

<?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>

      

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

      

+3


source to share


1 answer


You must pass resource_uri

as described in the doc:

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/

      



This is the part "user": "/api/v1/user/1/"

. I haven't worked with XML or m2m fields before, but it doesn't seem like you do to me.

I got the same error as you when I omitted this part.

0


source







All Articles