# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import migrations def event_to_gestion(apps, schema_editor): # Fetching the models that have be moved from cof to gestion OldEvent = apps.get_model('cof', 'Event') NewEvent = apps.get_model('gestion', 'Event') Location = apps.get_model('gestion', 'Location') Association = apps.get_model('gestion', 'Association') # The old Event.location field becomes a table: we need to create an entry # in this table for each value of the old `location` field. locations = set() # A set to prevent duplicate entries old_events = OldEvent.objects.values() new_events = [] for event in old_events: locations.add(event["location"]) new_events.append(event) Location.objects.bulk_create([Location(name=name) for name in locations]) map_loc = { loc.name: loc for loc in Location.objects.all() } for event in new_events: event["location"] = map_loc[event["location"]] NewEvent.objects.bulk_create([ NewEvent(**event) for event in new_events ]) # Do not forget to link all the existing event to the COF association cof_assoc = Association.objects.get(name="COF") cof_assoc.events.add(*NewEvent.objects.all()) # Migrating the other models is straightforward. # Keep care to the ordering. A change can lead to DB error because of # failed checks on foreignkey constraints. The dependencies between these # models give the following constraints: # - EventRegistration must precede EventCommentField and EventOption, # - EventCommentField must precede EventCommentValue, # - EventOption must precede EventOptionChoice. model_names = [ 'EventRegistration', 'EventCommentField', 'EventCommentValue', 'EventOption', 'EventOptionChoice', ] cls_models = [ (apps.get_model('cof', name), apps.get_model('gestion', name)) for name in model_names ] for FromModel, ToModel in cls_models: ToModel.objects.bulk_create([ ToModel(**values) for values in FromModel.objects.values() ]) class Migration(migrations.Migration): dependencies = [ ('cof', '0015_move_club'), ('gestion', '0002_create_cof_bds'), ] operations = [ migrations.RunPython(event_to_gestion), migrations.RemoveField( model_name='eventcommentfield', name='event', ), migrations.RemoveField( model_name='eventcommentvalue', name='commentfield', ), migrations.RemoveField( model_name='eventcommentvalue', name='registration', ), migrations.RemoveField( model_name='eventoption', name='event', ), migrations.RemoveField( model_name='eventoptionchoice', name='event_option', ), migrations.AlterUniqueTogether( name='eventregistration', unique_together=set([]), ), migrations.RemoveField( model_name='eventregistration', name='event', ), migrations.RemoveField( model_name='eventregistration', name='filledcomments', ), migrations.RemoveField( model_name='eventregistration', name='options', ), migrations.RemoveField( model_name='eventregistration', name='user', ), migrations.DeleteModel( name='Event', ), migrations.DeleteModel( name='EventCommentField', ), migrations.DeleteModel( name='EventCommentValue', ), migrations.DeleteModel( name='EventOption', ), migrations.DeleteModel( name='EventOptionChoice', ), migrations.DeleteModel( name='EventRegistration', ), ]