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.
 
 

32 lines
1.2 KiB

from django.db import migrations
import json
def convert_to_json(apps, schema_editor):
"""Convert CharField strings to JSON array of language objects"""
HadisCategory = apps.get_model('hadis', 'HadisCategory')
for obj in HadisCategory.objects.all():
# Convert plain string to JSON array with language objects
if obj.title and isinstance(obj.title, str):
# Wrap string in JSON format: [{"text": "string", "language_code": "en"}]
obj.title = [{"text": obj.title, "language_code": "en"}]
obj.save(update_fields=['title'])
def reverse_convert(apps, schema_editor):
"""Revert JSON back to plain text (optional)"""
HadisCategory = apps.get_model('hadis', 'HadisCategory')
for obj in HadisCategory.objects.all():
if obj.title and isinstance(obj.title, list) and len(obj.title) > 0:
# Extract first language's text
obj.title = obj.title[0].get('text', '')
obj.save(update_fields=['title'])
class Migration(migrations.Migration):
dependencies = [
('hadis', '0049_alter_transmitters_slug'), # Adjust to your last working migration
]
operations = [
migrations.RunPython(convert_to_json, reverse_convert),
]