kaauh_ats/recruitment/migrations/0007_migrate_profile_data_to_customuser.py

61 lines
1.8 KiB
Python

# Generated by Django 5.2.6 on 2025-11-15 20:57
from django.db import migrations
def migrate_profile_data_to_customuser(apps, schema_editor):
"""
Migrate data from Profile model to CustomUser model
"""
CustomUser = apps.get_model('recruitment', 'CustomUser')
Profile = apps.get_model('recruitment', 'Profile')
# Get all profiles
profiles = Profile.objects.all()
for profile in profiles:
if profile.user:
# Update CustomUser with Profile data
user = profile.user
if profile.profile_image:
user.profile_image = profile.profile_image
if profile.designation:
user.designation = profile.designation
user.save(update_fields=['profile_image', 'designation'])
def reverse_migrate_profile_data(apps, schema_editor):
"""
Reverse migration: move data from CustomUser back to Profile
"""
CustomUser = apps.get_model('recruitment', 'CustomUser')
Profile = apps.get_model('recruitment', 'Profile')
# Get all users with profile data
users = CustomUser.objects.exclude(profile_image__isnull=True).exclude(profile_image='')
for user in users:
# Get or create profile for this user
profile, created = Profile.objects.get_or_create(user=user)
# Update Profile with CustomUser data
if user.profile_image:
profile.profile_image = user.profile_image
if user.designation:
profile.designation = user.designation
profile.save(update_fields=['profile_image', 'designation'])
class Migration(migrations.Migration):
dependencies = [
('recruitment', '0006_add_profile_fields_to_customuser'),
]
operations = [
migrations.RunPython(
migrate_profile_data_to_customuser,
reverse_migrate_profile_data,
),
]