from django.core.management.base import BaseCommand from apps.blog.models import Blog class Command(BaseCommand): help = 'Populate empty title and slogan fields in Blog records with default JSON structures.' def handle(self, *args, **kwargs): blogs = Blog.objects.all() updated_count = 0 for blog in blogs: needs_update = False # Check if title is logically empty (None, [], {}, or "") if not blog.title: # Setting a default structure based on your model's docstring blog.title = [{"title": "Default Blog Title", "language_code": "en"}] needs_update = True # Check if slogan is logically empty if not blog.slogan: blog.slogan = [{"text": "Default Blog Slogan", "language_code": "en"}] needs_update = True if needs_update: # Use update_fields for performance and to prevent overriding other concurrent saves blog.save(update_fields=['title', 'slogan']) updated_count += 1 self.stdout.write(self.style.SUCCESS(f'Successfully checked all blogs and updated {updated_count} records.'))