You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

30 lines
1.2 KiB

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