From 0a9ecd8c9632c5a1603c41d0957e4766796ee375 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 16 Mar 2018 13:55:15 +0100 Subject: [PATCH] Allow CorpContact.corporation being null --- .../migrations/0009_corpcontact_corp_nullable.py | 16 ++++++++++++++++ stages/models.py | 7 +++++-- stages/views.py | 3 ++- 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 stages/migrations/0009_corpcontact_corp_nullable.py diff --git a/stages/migrations/0009_corpcontact_corp_nullable.py b/stages/migrations/0009_corpcontact_corp_nullable.py new file mode 100644 index 0000000..4a68874 --- /dev/null +++ b/stages/migrations/0009_corpcontact_corp_nullable.py @@ -0,0 +1,16 @@ +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('stages', '0008_add_logbook'), + ] + + operations = [ + migrations.AlterField( + model_name='corpcontact', + name='corporation', + field=models.ForeignKey(blank=True, null=True, on_delete=models.deletion.CASCADE, to='stages.Corporation', verbose_name='Institution'), + ), + ] diff --git a/stages/models.py b/stages/models.py index a04c399..0031a0a 100644 --- a/stages/models.py +++ b/stages/models.py @@ -311,7 +311,10 @@ class Corporation(models.Model): class CorpContact(models.Model): - corporation = models.ForeignKey(Corporation, verbose_name='Institution', on_delete=models.CASCADE) + corporation = models.ForeignKey( + Corporation, verbose_name='Institution', null=True, blank=True, + on_delete=models.CASCADE + ) ext_id = models.IntegerField(null=True, blank=True, verbose_name='ID externe') is_main = models.BooleanField(default=False, verbose_name='Contact principal') always_cc = models.BooleanField(default=False, verbose_name='Toujours en copie') @@ -328,7 +331,7 @@ class CorpContact(models.Model): verbose_name = "Contact" def __str__(self): - return '{0} {1}, {2}'.format(self.last_name, self.first_name, self.corporation) + return '{0} {1}, {2}'.format(self.last_name, self.first_name, self.corporation or '-') class Domain(models.Model): diff --git a/stages/views.py b/stages/views.py index 986d983..2c09940 100644 --- a/stages/views.py +++ b/stages/views.py @@ -751,7 +751,8 @@ def stages_export(request, scope=None): (c, {s: [] for s in section_names}) for c in Corporation.objects.all().values_list('name', flat=True) ) - for contact in CorpContact.objects.all().select_related('corporation' + for contact in CorpContact.objects.filter(corporation__isnull=False + ).select_related('corporation' ).prefetch_related('sections').order_by('corporation'): for section in contact.sections.all(): if not default_contacts[contact.corporation.name][section.name] or contact.is_main is True: