Search code examples
pythondjangotastypie

Error in posting to model with foreignkey django-tastypie


I am getting error when making a post request to api in my django webapp , I am using tastypie for api and getting following error.

post request

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

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

Solution

  • You post data:

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

    change artist property to:

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

    or the url of resource:

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