【发布时间】:2013-05-22 06:44:30
【问题描述】:
我正在用 django 构建一个相册应用,我有两个 django 模型
class Album(models.Model):
name = models.CharField(max_length=100)
family = models.ForeignKey(FamilyProfile)
created_by = models.ForeignKey(User)
created_date = models.DateField(default=datetime.datetime.now())
class Image(models.Model):
album = models.ForeignKey(Album)
name = models.CharField(max_length=100,null=True,blank=True)
src = models.ImageField(upload_to=MEDIA_ROOT)
upload_by = models.ForeignKey(User)
upload_time = models.DateTimeField(default=datetime.datetime.now())
并为 RESFull api 使用 Tastepir,
在主干中我有两个集合
album.albumCollection = Backbone.Tastypie.Collection.extend({
url:'/album/v1/album/',
model:album.albumModel,
})
image.imageCollection = Backbone.Tastypie.Collection.extend({
url:'/album/v1/image/',
model:image.imageModel,
})
和骨干路由器
album.router = Backbone.Router.extend({
routes:{
'':'album',
'test/:id':'openAlbum',
},
album:function(){
this.albums = new album.albumCollection()
this.albumsView = new album.albumCollectionView({model:this.albums})
this.albums.fetch({reset: true})
},
openAlbum:function(id){
this.images = new image.imageCollection()
this.imagesView = new image.imageCollectionView({model:this.albums})
this.images.fetch({reset: true})
}
})
new album.router();
Backbone.history.start();
我可以渲染相册,但是当我尝试打开相册时,如您所见,我需要通过相册的 id 获取图像集合,但 imageCollection.url 是静态的。
如何获取带有骨干和美味派的相册中的图像?
【问题讨论】:
-
Tastypie 可以响应数据库中的任何数据,甚至是关系数据,您可以发送 json loke 这个 {album_id: {images:[{url:'', title:'', etc}, ...] }}
-
当然可以,但是如何获取相册[id]的图片
-
我的意思是如何编写imageCollection url来获取相册[id]中的所有图片
-
/api/v1/album/[id] ???
标签: javascript django backbone.js tastypie