from datetime import datetime, time from django.db import migrations from django.utils import timezone def backfill_timestamps(apps, schema_editor): IotPanel = apps.get_model("operations", "IotPanel") for row in IotPanel.objects.filter(timestamp__isnull=True): row.timestamp = timezone.make_aware( datetime.combine(row.date, time(0, 0)), timezone.get_current_timezone() ) row.save(update_fields=["timestamp"]) def unset_timestamps(apps, schema_editor): IotPanel = apps.get_model("operations", "IotPanel") IotPanel.objects.update(timestamp=None) class Migration(migrations.Migration): dependencies = [ ("operations", "0006_iotpanel_timestamp"), ] operations = [ migrations.RunPython(backfill_timestamps, unset_timestamps), ]