diff --git a/factories/indicators_models.py b/factories/indicators_models.py index f291ad4ce..bfd4a1c70 100644 --- a/factories/indicators_models.py +++ b/factories/indicators_models.py @@ -68,6 +68,17 @@ def workflowlevel1(self, create, extracted, **kwargs): for workflowlevel1 in extracted: self.workflowlevel1.add(workflowlevel1) + @post_generation + def disaggregation(self, create, extracted, **kwargs): + if not create: + # Simple build, do nothing. + return + + if extracted: + # A list of disaggregation were passed in, use them + for disaggregation in extracted: + self.disaggregation.add(disaggregation) + class Objective(DjangoModelFactory): class Meta: diff --git a/indicators/migrations/0009_auto_20180509_0140.py b/indicators/migrations/0009_auto_20180509_0140.py new file mode 100644 index 000000000..134b9f0d1 --- /dev/null +++ b/indicators/migrations/0009_auto_20180509_0140.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.3 on 2018-05-09 08:40 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('indicators', '0008_disaggregationvalue_indicator'), + ] + + operations = [ + migrations.AlterField( + model_name='disaggregationlabel', + name='disaggregation_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='indicators.DisaggregationType'), + ), + migrations.AlterField( + model_name='disaggregationvalue', + name='disaggregation_label', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='indicators.DisaggregationLabel'), + ), + ] diff --git a/indicators/models.py b/indicators/models.py index d3125b33f..ff4db3102 100755 --- a/indicators/models.py +++ b/indicators/models.py @@ -192,7 +192,7 @@ class DisaggregationTypeAdmin(admin.ModelAdmin): class DisaggregationLabel(models.Model): - disaggregation_type = models.ForeignKey(DisaggregationType) + disaggregation_type = models.ForeignKey(DisaggregationType, on_delete=models.deletion.PROTECT) label = models.CharField(max_length=765, blank=True) customsort = models.IntegerField(blank=True, null=True) create_date = models.DateTimeField(null=True, blank=True) @@ -210,7 +210,7 @@ def save(self, *args, **kwargs): class DisaggregationValue(models.Model): - disaggregation_label = models.ForeignKey(DisaggregationLabel) + disaggregation_label = models.ForeignKey(DisaggregationLabel, on_delete=models.deletion.PROTECT) indicator = models.ForeignKey('indicators.Indicator', null=True, blank=True) value = models.CharField(max_length=765, blank=True) create_date = models.DateTimeField(null=True, blank=True)