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.'))