from __future__ import unicode_literals from django.db import migrations def clubs_to_gestion(apps, schema_editor): Association = apps.get_model('gestion', 'Association') Membership = apps.get_model('gestion', 'ClubUser') OldClub = apps.get_model('cof', 'Club') NewClub = apps.get_model('gestion', 'Club') cof_assoc = Association.objects.get(name='COF') memberships = [] for oldclub in OldClub.objects.all(): newclub = NewClub.objects.create( name=oldclub.name, description=oldclub.description, association=cof_assoc, ) members = oldclub.membres.values_list('id', flat=True) respos = oldclub.respos.values_list('id', flat=True) for user in members: memberships.append( Membership( club=newclub, user_id=user, has_paid=True, is_respo=user in respos, ) ) Membership.objects.bulk_create(memberships) class Migration(migrations.Migration): """ This migration focus on migrating clubs data to the 'gestion' app. """ dependencies = [ ('cof', '0014_move_profile'), ('gestion', '0002_create_cof_bds'), ] operations = [ # Move clubs from cof to gestion. migrations.RunPython(clubs_to_gestion), # Delete legacy Club model. migrations.RemoveField( model_name='club', name='membres', ), migrations.RemoveField( model_name='club', name='respos', ), migrations.DeleteModel( name='Club', ), ]