import urllib2 from django.conf import settings from django.utils import translation from django.core.management.base import BaseCommand, CommandError from place_exposition.models import PlaceExposition def handle_place(place): logo = place.get_logo() translation.activate('en') domain = 'http://hit.expomap.ru' url = domain+logo.file_path.url file_name = url.split('/')[-1] download_to = settings.MEDIA_ROOT+'place_exposition/logo/'+file_name try: response = urllib2.urlopen(url, timeout=5) except: print('download error') return None with open(download_to,'wb') as f: f.write(response.read()) f.close() file_name = 'place_exposition/logo/'+file_name place.logo = file_name place.save() class Command(BaseCommand): def handle(self, *args, **options): for place in PlaceExposition.objects.all('en'): if place.files.filter(purpose='logo').exists(): handle_place(place) print(place)