0

在我的 django webapp 中向 api 发出 post 请求时出现错误,我正在为 api 使用tastepie 并出现以下错误。

发布请求

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"title": "Post Title 1", "video": "http://www.youtube.com/watch?v=0u03h73ClZ8", "artist": "artist_name 1"}' "http://127.0.0.1:8000/api/v1/video/" -u "username:password"

错误

"error_message":{"error_message": "The URL provided 'artist_name 1' was not a link to a valid resource.", "traceback": "Traceback (most recent call last):\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 195, in wrapper\n    response = callback(request, *args, **kwargs)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 426, in dispatch_list\n    return self.dispatch('list', request, **kwargs)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 458, in dispatch\n    response = method(request, **kwargs)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 1320, in post_list\n    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 2083, in obj_create\n    bundle = self.full_hydrate(bundle)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 876, in full_hydrate\n    value = field_object.hydrate(bundle)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/fields.py\", line 739, in hydrate\n    return self.build_related_resource(value, request=bundle.request)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/fields.py\", line 655, in build_related_resource\n    return self.resource_from_uri(self.fk_resource, value, **kwargs)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/fields.py\", line 574, in resource_from_uri\n    obj = fk_resource.get_via_uri(uri, request=request)\n\n  File \"/home/w0w/lmvapp/local/lib/python2.7/site-packages/tastypie/resources.py\", line 802, in get_via_uri\n    raise NotFound(\"The URL provided '%s' was not a link to a valid resource.\" % uri)\n

模型.py

class Artist(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return unicode(self.name)

class Video(models.Model):
    title = models.CharField(max_length=255)
# url Should be a valid youtube URL
    video = EmbedVideoField()
    artist = models.ForeignKey(Artist)
    slug = AutoSlugField(populate_from='title', unique=True)

     # SLUG depends on title
    def __unicode__(self):
    return unicode(self.title)

api.py

class ArtistResource(ModelResource):
    class Meta:
        queryset = Artist.objects.all()
        resource_name = 'artist'

class VideoResource(ModelResource):
    artist = fields.ForeignKey(ArtistResource, 'artist')

    class Meta:
         queryset = Video.objects.all()
         resource_name = 'video'
         authorization = DjangoAuthorization()
         authentication = BasicAuthentication()
4

1 回答 1

2

您发布数据:

{
    "title": "Post Title 1",
    "video": "http://www.youtube.com/watch?v=0u03h73ClZ8",
    "artist": "artist_name 1"
}

将艺术家属性更改为:

"artist": {"name": "artist_name 1"}

或资源的网址:

"artist": "/api/v1/user/1/"
于 2013-10-27T14:26:38.327 回答