From 7be3c2b93faf6ea2d658612c0bba674900676296 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 31 Oct 2019 12:02:52 +0300 Subject: [PATCH] add_establishment_description command --- .../commands/add_establishment_description.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 apps/establishment/management/commands/add_establishment_description.py diff --git a/apps/establishment/management/commands/add_establishment_description.py b/apps/establishment/management/commands/add_establishment_description.py new file mode 100644 index 00000000..1748d81d --- /dev/null +++ b/apps/establishment/management/commands/add_establishment_description.py @@ -0,0 +1,31 @@ +from django.core.management.base import BaseCommand + +from establishment.models import Establishment +from transfer.models import Descriptions + + +class Command(BaseCommand): + help = 'Add description values from old db to new db' + + def handle(self, *args, **kwargs): + count = 0 + + queryset = Descriptions.objects.all() + for obj in queryset: + try: + establishment = Establishment.objects.get(old_id=obj.establishment.id) + except Establishment.DoesNotExist: + continue + except Establishment.MultipleObjectsReturned: + establishment = Establishment.objects.filter(old_id=obj.establishment.id).first() + else: + description = establishment.description + description.update({ + obj.locale: obj.text + }) + establishment.description = description + establishment.save() + count += 1 + break + + self.stdout.write(self.style.WARNING(f'Updated {count} objects.'))