def savePhoto(self, galleryId, photoId, photoName, photoDescription, photoPosition, file): photo = None if(photoId): photo = Photo.objects.get(id=photoId) else: gid = uuid.uuid1().__str__() gid = gid.replace('-', '') photo = Photo() photo.id = gid photo.name = photoName photo.description = photoDescription photo.position = photoPosition photo.gallery_id = galleryId if(file): photoType = file.name.split('.') photo.type = photoType[1] if photoType.__len__() == 2 else '' destination = open(constant.upload_path + galleryId + '/' + photo.id + '.' + photo.type, 'wb') try: for chunk in file.chunks(): destination.write(chunk) finally: destination.close() photo.save()