from django.core.management.base import BaseCommand from django.db.models import F from tqdm import tqdm from account.models import User from news.models import News from transfer.models import PageTexts class Command(BaseCommand): help = 'Add author of News' def handle(self, *args, **kwargs): count = 0 news_list = News.objects.filter(created_by__isnull=True) for news in tqdm(news_list, desc="Find author for exist news"): old_news = PageTexts.objects.filter(id=news.old_id).annotate( account_id=F('page__account_id'), ).first() if old_news: user = User.objects.filter(old_id=old_news.account_id).first() if user: news.created_by = user news.modified_by = user news.save() count += 1 self.stdout.write(self.style.WARNING(f'Update {count} objects.'))