开发者

How to delete an object and all related objects with all imageFields insite them (photo gallery)

I have a photo gallery with an album model (just title and date and stuff) and a photo model with a foriegn key to the album and three imageFields in it (regular, mid and thumb).

When a user delete an album i need to delete all the photos reletaed to the album (from server) then all the DB records that point to the album and then the album itself...

Couldn't find anything about this and actualy found so many answers what one say the oposite from the other.

Can any one please c开发者_Go百科larify this point, how does this is beeing done in the real world?

Thank you very much, Erez


Here is a possible answer for the question i figured out:

  1. Getting the list of albums in a string, in my case separated by commas
  2. You need to import shutil, then:

    @login_required def remove_albums(request): if request.is_ajax(): if request.method == 'POST': #if the ajax call for delete what ok we get the list of albums to delete albums_list = request.REQUEST['albums_list'].rsplit(',') for album in albums_list: obj_album = Album.objects.get(id=int(album)) #getting the directory for the images than need to be deleted dir_path = MEDIA_ROOT + '/images/galleries/%d' % obj_album.id #deleting the DB record obj_album.delete() #testing if there is a folder (there might be a record with no folder if no file was uploaded - deleting the album before uploading images) try: #deleting the folder and all the files in it shutil.rmtree(dir_path) except OSError: pass return HttpResponse('')

Sorry for how the code look like, don't know why, I can't make it show correct...

Have fun and good luck :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜