def handle(self, *args, **options): with open('photos.pkl') as file: self.data = pickle.load(file) for each in self.data: id = each.get('id') url = each.get('url') product = self._get_product(id) filename = self._get_filename(url) photo = Photo(product=product) if filename in os.listdir(PHOTO_DIR): photo_path = '/'.join(['photos', filename]) photo.file = photo_path else: file_content = self._download(url) photo.file.save(filename, file_content) photo.save()