# Generated migration to fix null username values from django.db import migrations def fix_null_username(apps, schema_editor): """Set username to email for users with null username""" User = apps.get_model('accounts', 'User') # Update all users with null username to use their email for user in User.objects.filter(username__isnull=True): user.username = user.email user.save(update_fields=['username']) def reverse_fix_null_username(apps, schema_editor): """Reverse migration: set username back to None""" User = apps.get_model('accounts', 'User') User.objects.all().update(username=None) class Migration(migrations.Migration): dependencies = [ ('accounts', '0002_initial'), ] operations = [ migrations.RunPython(fix_null_username, reverse_fix_null_username), ]