2013-10-27 55 views
0

我在django webapp中發送對api的發佈請求時出現錯誤,我正在使用tastypie來獲取跟蹤錯誤。在發佈到foreignkey的模型時發生錯誤django-tastypie

post請求

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 

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

回答

2

您發佈的數據:

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

變化藝術家屬性爲:

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

或資源的URL:

"artist": "/api/v1/user/1/" 
+0

您的文章數據。 – iMom0

相關問題