python - How to delete an object and all related objects with all imageFields insite them (photo gallery) -
i have photo gallery album model (just title , date , stuff) , photo model foriegn key album , 3 imagefields in (regular, mid , thumb).
when user delete album need delete photos reletaed album (from server) db records point album , album itself...
couldn't find , actualy found many answers 1 oposite other.
can 1 please clarify point, how beeing done in real world?
thank much, erez
here possible answer question figured out:
- getting list of albums in string, in case separated commas
you need import shutil, then:
@login_required def remove_albums(request): if request.is_ajax(): if request.method == 'post': #if ajax call delete ok list of albums delete albums_list = request.request['albums_list'].rsplit(',') album in albums_list: obj_album = album.objects.get(id=int(album)) #getting directory images need deleted dir_path = media_root + '/images/galleries/%d' % obj_album.id #deleting db record obj_album.delete() #testing if there folder (there might record no folder if no file uploaded - deleting album before uploading images) try: #deleting folder , files in shutil.rmtree(dir_path) except oserror: pass return httpresponse('')
sorry how code like, don't know why, can't make show correct...
have fun , luck :-)
Comments
Post a Comment