Browse Source

blogs schema changed

title and slogan required now

script added for making default title and slogan for required objects
master
Mohsen Taba 4 weeks ago
parent
commit
5225450545
  1. 7
      apps/blog/admin.py
  2. 30
      apps/blog/management/commands/fix_empty_blog_fields.py
  3. 23
      apps/blog/migrations/0002_alter_blog_slogan_alter_blog_title.py
  4. 4
      apps/blog/models.py

7
apps/blog/admin.py

@ -31,6 +31,13 @@ class BlogAdminForm(forms.ModelForm):
'summary': MultiLanguageJSONWidget(input_widget_class=WysiwygWidget), 'summary': MultiLanguageJSONWidget(input_widget_class=WysiwygWidget),
'slug': MultiLanguageJSONWidget(input_widget_class=UnfoldAdminTextInputWidget), 'slug': MultiLanguageJSONWidget(input_widget_class=UnfoldAdminTextInputWidget),
} }
# ADD THIS METHOD:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Explicitly tell the form these fields are required
# so the admin template renders the red star
self.fields['title'].required = True
self.fields['slogan'].required = True
class BlogContentInline(StackedInline): class BlogContentInline(StackedInline):

30
apps/blog/management/commands/fix_empty_blog_fields.py

@ -0,0 +1,30 @@
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.'))

23
apps/blog/migrations/0002_alter_blog_slogan_alter_blog_title.py

@ -0,0 +1,23 @@
# Generated by Django 5.2.12 on 2026-04-26 11:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='blog',
name='slogan',
field=models.JSONField(default=list, verbose_name='slogan'),
),
migrations.AlterField(
model_name='blog',
name='title',
field=models.JSONField(default=list, verbose_name='title'),
),
]

4
apps/blog/models.py

@ -9,13 +9,13 @@ class Blog(models.Model):
""" """
Blog model with title, thumbnail, slogan, summary, views count and timestamps Blog model with title, thumbnail, slogan, summary, views count and timestamps
""" """
title = models.JSONField(default=list, null=True, blank=True, verbose_name=_('title')) # [{"title": "", "language_code": "en"},{"title": "", "language_code": "fa"},...]
title = models.JSONField(default=list, null=False, blank=False, verbose_name=_('title')) # [{"title": "", "language_code": "en"},{"title": "", "language_code": "fa"},...]
thumbnail = models.ImageField( thumbnail = models.ImageField(
upload_to='blog/thumbnails/%Y/%m/', upload_to='blog/thumbnails/%Y/%m/',
verbose_name=_('Thumbnail'), verbose_name=_('Thumbnail'),
help_text=_('Blog thumbnail image') help_text=_('Blog thumbnail image')
) )
slogan = models.JSONField(default=list, null=True, blank=True, verbose_name=_('slogan'))
slogan = models.JSONField(default=list, null=False, blank=False, verbose_name=_('slogan'))
summary = models.JSONField(default=list, null=True, blank=True, verbose_name=_('summary')) summary = models.JSONField(default=list, null=True, blank=True, verbose_name=_('summary'))
views_count = models.PositiveIntegerField( views_count = models.PositiveIntegerField(
default=0, default=0,

Loading…
Cancel
Save