46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
# Generated manually for multi-commune contracts
|
|
|
|
from django.db import migrations, models
|
|
|
|
|
|
def copy_municipality_to_municipalities(apps, schema_editor):
|
|
Contract = apps.get_model('contracts', 'Contract')
|
|
for contract in Contract.objects.all():
|
|
if getattr(contract, 'municipality_id', None):
|
|
contract.municipalities.add(contract.municipality_id)
|
|
|
|
|
|
def reverse_copy(apps, schema_editor):
|
|
Contract = apps.get_model('contracts', 'Contract')
|
|
for contract in Contract.objects.all():
|
|
first_m = contract.municipalities.first()
|
|
if first_m:
|
|
contract.municipality_id = first_m.id
|
|
contract.save(update_fields=['municipality_id'])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('common', '0015_remove_obsolete_schools_appview'),
|
|
('contracts', '0010_contract_municipality'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name='contract',
|
|
name='municipalities',
|
|
field=models.ManyToManyField(
|
|
blank=True,
|
|
help_text='Communes couvertes par ce contrat (permet le filtrage et le cadrage cartographique).',
|
|
related_name='contracts',
|
|
to='common.municipality',
|
|
verbose_name='Communes associées',
|
|
),
|
|
),
|
|
migrations.RunPython(copy_municipality_to_municipalities, reverse_code=reverse_copy),
|
|
migrations.RemoveField(
|
|
model_name='contract',
|
|
name='municipality',
|
|
),
|
|
]
|