from django.core.management.base import BaseCommand, CommandError from theme.models import Theme, ThemeBlog from article.models import Article class Command(BaseCommand): def handle(self, *args, **options): for article in Article.objects.filter(type=Article.blog).prefetch_related('blog_theme'): blogthemes = article.blog_theme.all().values_list('id', flat=True) themes = Theme.objects.filter( types=Theme.types.article, blogtheme_linking_id__in=blogthemes) article.theme.clear() article.theme.add(*themes)