错误:对象没有属性' get_via_uri'"在tastypie中的post对象中

时间:2014-07-15 17:58:15

标签: python django tastypie

我是django在发布数据时遇到错误的新手:

{    "name": "My family",
     "title": "Wassup",
     "gallery":"/api/v1/gallery/1/"
}

到终点:http://127.0.0.1:8000/api/v1/images/
" error_message":"'图库'对象没有属性' get_via_uri'"

models.py

class Gallery(models.Model):
    name = models.CharField(max_length=64)

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


class Image(models.Model):
    gallery = models.ForeignKey(Gallery)
    name = models.CharField(max_length=64)
    title = models.CharField(max_length=255)

resources.py

class GalleryResource(ModelResource):
    images = fields.ToManyField(
        'gallery_app.resources.ImageResource',
        'images',
        full=True)

    class Meta:
        queryset = Gallery.objects.all()
        authorization = Authorization()
        resource_name = 'gallery'


class ImageResource(ModelResource):
    gallery = fields.ForeignKey(Gallery, 'gallery')

    class Meta:
        queryset = Image.objects.all()
        authorization = Authorization()
        resource_name = 'images'

冻结

Django==1.6.5
Pillow==2.5.1
South==1.0
argparse==1.2.1
django-tastypie==0.11.1
python-dateutil==2.2
python-mimeparse==0.1.4
six==1.7.3
wsgiref==0.1.2

请建议基于tastypie的api建立一些好的项目以供参考。谢谢。

1 个答案:

答案 0 :(得分:0)

您指的是ImageResource中的模型库,您应该引用GalleryResource。

class ImageResource(ModelResource):
  gallery = fields.ForeignKey(GalleryResource, 'gallery')

  class Meta:
    queryset = Image.objects.all()
    authorization = Authorization()
    resource_name = 'images'

参考:http://django-tastypie.readthedocs.io/en/latest/resources.html#reverse-relationships