使用django-tasypie时,有哪种方法可以在资源网址之外添加相关对象的额外字段?
我有相册模型和照片模型。
class Album(models.Model):
title = models.CharField(_('Album Name'), max_length=250, default=_('Untitled Album'))
class Photo(models.Model):
album = models.ForeignKey(Album)
我在api中将它们作为ModelResources:
class PhotoResource(ModelResource):
album = fields.ToOneField('ampwefly.apps.photos.api.AlbumResource', 'album')
class AlbumResource(ModelResource):
photo = fields.ToManyField(PhotoResource, 'photo_set', related_name='photo', full=True)
目前我可以在访问单张照片的网址时返回以下内容:
{
album: "/api/v1/album/2/",
description: "",
id: "1",
image: "/uploads/uploads/users/images/e-jey.jpg",
resource_uri: "/api/v1/photo/1/",
uploaded: "2012-11-22T22:42:26.051779"
}
如果我使用:
album = fields.ToOneField('ampwefly.apps.photos.api.AlbumResource', 'album', full=True)
这将返回相册的所有字段,但我想尝试做的是除了资源网址之外还返回标题,例如:
{
album: {
id: "2",
resource_uri: "/api/v1/album/2/",
title: "Untitled Album"
},
description: "",
id: "1",
image: "/uploads/uploads/users/images/e-jey.jpg",
resource_uri: "/api/v1/photo/1/",
uploaded: "2012-11-22T22:42:26.051779"
}
获得上述结果的建议解决方案是什么?