0014_auto_20170614_2242.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # -*- coding: utf-8 -*-
  2. # Generated by Django 1.11.2 on 2017-06-14 20:42
  3. from __future__ import unicode_literals
  4. from django.db import migrations, models
  5. import django.db.models.deletion
  6. def migrate_adhesions(apps, schema_editor):
  7. User = apps.get_model("adhesions", "User")
  8. Corporation = apps.get_model("adhesions", "Corporation")
  9. Adhesion = apps.get_model("adhesions", "Adhesion")
  10. ContentType = apps.get_model("contenttypes", "ContentType")
  11. user_type = ContentType.objects.get_for_model(User)
  12. corp_type = ContentType.objects.get_for_model(Corporation)
  13. db_alias = schema_editor.connection.alias
  14. for adh in Adhesion.objects.using(db_alias).all():
  15. if adh.adherent_type == user_type:
  16. adh.user = User.objects.using(db_alias).get(pk=adh.adherent_id)
  17. else:
  18. assert(adh.adherent_type == corp_type)
  19. adh.corporation = Corporation.objects.using(db_alias).get(pk=adh.adherent_id)
  20. adh.save()
  21. class Migration(migrations.Migration):
  22. dependencies = [
  23. ('adhesions', '0013_adhesion_active'),
  24. ('accounts', '0006_auto_20170607_2306'),
  25. ]
  26. operations = [
  27. migrations.AddField(
  28. model_name='adhesion',
  29. name='user',
  30. field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='adhesions.User'),
  31. ),
  32. migrations.AddField(
  33. model_name='adhesion',
  34. name='corporation',
  35. field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, to='adhesions.Corporation'),
  36. ),
  37. migrations.RunPython(migrate_adhesions),
  38. ]