From 5d76e8a02bce6c9a5acef222ebde496ee83f63af Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 6 Dec 2019 08:20:23 +0300 Subject: [PATCH] fix address command --- .../management/commands/fix_address.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 apps/location/management/commands/fix_address.py diff --git a/apps/location/management/commands/fix_address.py b/apps/location/management/commands/fix_address.py new file mode 100644 index 00000000..5fcdf1b9 --- /dev/null +++ b/apps/location/management/commands/fix_address.py @@ -0,0 +1,29 @@ +from django.core.management.base import BaseCommand +from tqdm import tqdm + +from location.models import Address +from transfer.models import Locations + + +class Command(BaseCommand): + help = """Fix address, clear number field and fill street_name_1 like in old db""" + + def handle(self, *args, **kwarg): + addresses = Address.objects.filter( + old_id__isnull=False + ).values_list('old_id', flat=True) + + old_addresses = Locations.objects.filter( + id__in=list(addresses) + ).values_list('id', 'address') + + update_address = [] + for idx, address in tqdm(old_addresses): + new_address = Address.objects.filter(old_id=idx).first() + if new_address: + new_address.number = 0 + new_address.street_name_1 = address + update_address.append(new_address) + + Address.objects.bulk_update(update_address, ['number', 'street_name_1']) + self.stdout.write(self.style.WARNING(f'Updated addresses: {len(update_address)}'))