0
votes

I'm building an album app in django, I have two django model

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

and using tastypir for RESFull api,

in backbone I have two collections

   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,
   })

and backbone router

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

I can get the album rendered, but when I try to open the album, as you can see I need to fetch the image collection by the album's id, but the imageCollection.url is static.

how can I get the images inside a album with backbone and tastypie?

1
Tastypie can response with any data from db even relational and you can send json loke this {album_id: {images:[{url:'', title:'', etc}, ...]}}Denis
sure, but how to get the images of album[id]paynestrike
I mean how to write the imageCollection url to fetch all the images in album[id]paynestrike
/api/v1/album/[id] ???Denis

1 Answers

0
votes

Setup a filtering on album field of Image tastypie resource and change the url to filter images by album:

openAlbum:function(id){
    this.images = new image.imageCollection()
    this.images.url = this.images.url + "?album=" + id;
    this.imagesView = new image.imageCollectionView({model:this.images})
    this.images.fetch({reset: true})
}