- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个资源 (Tastypie),其中一个具有 ToManyField
字段:
class SongResource(ModelResource):
class Meta:
queryset = Song.objects.all()
resource_name = 'song'
authorization = Authorization()
class AlbumResource(ModelResource):
songs = fields.ToManyField('core.api.SongResource', 'songs', full=True)
class Meta:
queryset = Album.objects.all()
resource_name = 'album'
authorization = Authorization()
因此,当我访问 PlaylistResource 时,我会看到如下内容:
http://127.0.0.1:8000/api/v1/playlist/1/?format=json
这是我得到的响应:
{
"created_in": "2012-06-24T22:57:01+00:00",
"id": "1",
"number_of_plays": 0,
"playlist_slug": "my-first-playlist",
"playlist_title": "my first playlist",
"resource_uri": "/api/v1/playlist/1/",
"songs": [{
"genre": "Progressive metal",
"id": "2",
"length": "04:19",
"number_of_plays": 0,
"price": 0.0,
"resource_uri": "/api/v1/song/2/",
"song_slug": "leyla-2008",
"song_title": "Leyla",
"song_url": "http://www.amazon.s3.com/prologue",
"thumbnail": "http://almacosta.files.wordpress.com/2011/05/bob_marley.jpg?w=250",
"year": 2008
}, {
"genre": "Progressive metal",
"id": "3",
"length": "3.26",
"number_of_plays": 0,
"price": 0.0,
"resource_uri": "/api/v1/song/3/",
"song_slug": "yazamadim-2008",
"song_title": "Yazamadim",
"song_url": "http://www.amazon.s3.com/prologue",
"thumbnail": "http://img72.imageshack.us/img72/3215/14so8.jpg",
"year": 2008
}]
}
现在我想访问每首歌的专辑或艺术家,所以我添加了
album = fields.ForeignKey('core.api.AlbumResource','album')
到我的SongResource
,但我收到此错误
{
"error_message": "'Song' object has no attribute 'album'",
"traceback": "Traceback (most recent call last):\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 192, in wrapper\n response = callback(request, *args, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 406, in dispatch_detail\n return self.dispatch('detail', request, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 427, in dispatch\n response = method(request, **kwargs)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 1058, in get_detail\n bundle = self.full_dehydrate(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 654, in full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/fields.py\", line 709, in dehydrate\n m2m_dehydrated.append(self.dehydrate_related(m2m_bundle, m2m_resource))\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/fields.py\", line 514, in dehydrate_related\n return related_resource.full_dehydrate(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/resources.py\", line 654, in full_dehydrate\n bundle.data[field_name] = field_object.dehydrate(bundle)\n\n File \"/usr/local/lib/python2.7/dist-packages/tastypie/fields.py\", line 621, in dehydrate\n foreign_obj = getattr(bundle.obj, self.attribute)\n\nAttributeError: 'Song' object has no attribute 'album'\n"
}
有什么技巧可以让我获取反向数据吗?
编辑这是我的歌曲和专辑模型(与 models.py 文件中的顺序相同)
class Song(models.Model):
song_title = models.CharField(max_length=140)
length = models.CharField(max_length=5, blank=True, null = True)
genre = models.CharField(max_length=100, blank = True, null = True)
year = models.IntegerField(blank = True, null = True)
thumbnail = models.URLField(default = '/media/thumbnail.png')
song_url = models.URLField()
number_of_plays = models.IntegerField(verbose_name='Number of plays')
price = models.FloatField(default = 0.0)
song_slug = models.SlugField(unique=True, max_length=200)
def __unicode__(self):
return self.song_title
class Album(models.Model):
album_title = models.CharField(max_length=140)
release_year = models.IntegerField('Release Year')
price = models.FloatField(default=0.0)
album_slug = models.SlugField(unique=True)
songs = models.ManyToManyField(Song)
def __unicode__(self):
return self.album_title
def get_absolute_url(self):
return '/album/%s' % self.album_slug
最佳答案
使用ToManyField
而不是ForeignKey并告诉它属性是album_set
。要获取要包含的完整资源,请指定 full=True
。
关于django - 反向关系 ToManyField Tastypie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11399342/
我有两个资源 (Tastypie),其中一个具有 ToManyField 字段: class SongResource(ModelResource): class Meta:
在 resources.py 我有: class CategoryResource(ModelResource): items = fields.ToManyField('ItemResour
我有一个使用 Django Tastypie 的 REST API。给定以下代码 模型 class BlogPost(models.Model): # class body omitted,
我在添加与 ApiKey 用户相关的资源时遇到了一些问题,问题正是“出租车”字段,当我评论说时,“create_object”工作正常。 有资源 资源 class LocationResource(M
我正在使用 Tastypie 为我的 Django (v1.5) 应用程序实现 API。我希望能够过滤/限制我在父资源时获得的相关资源。 这是我的(简化的)模型: # myapp/models.py
我有以下资源: class MachineResource(ModelResource): manager = fields.ToOneField(UserResource, 'manager',fu
在我的应用程序中,我有“评论”(实际上不完全是,但这是一种简单的思考方式),其他用户可以对评论进行投票(目前,这只是一个简单的模型,用户只需添加+1,或删除它,就像 Facebook 的“赞”或 Go
我是一名优秀的程序员,十分优秀!