From fe4e47edb07eb45ed5e14a20f4e6a10fdf203622 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Sun, 4 Mar 2018 12:48:35 +0100 Subject: [PATCH] add a related_name for the camp relation for TeamArea, and a teams property to Camp model to make it easier to get all teams under all teamareas, also add exclude=None to the validate_unique call in our CleanedModel class --- src/camps/models.py | 7 +++++++ .../migrations/0020_auto_20180304_1233.py | 21 +++++++++++++++++++ src/teams/models.py | 2 +- src/utils/models.py | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/teams/migrations/0020_auto_20180304_1233.py diff --git a/src/camps/models.py b/src/camps/models.py index 809dcc32..e9886edd 100644 --- a/src/camps/models.py +++ b/src/camps/models.py @@ -188,3 +188,10 @@ class Camp(CreatedUpdatedModel, UUIDModel): return False else: return True + + @property + def teams(self): + """ Return a queryset with all teams under all TeamAreas under this Camp """ + from teams.models import Team + return Team.objects.filter(area__in=self.teamareas.all()) + diff --git a/src/teams/migrations/0020_auto_20180304_1233.py b/src/teams/migrations/0020_auto_20180304_1233.py new file mode 100644 index 00000000..4cfe13a0 --- /dev/null +++ b/src/teams/migrations/0020_auto_20180304_1233.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2018-03-04 11:33 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('teams', '0019_auto_20180304_1019'), + ] + + operations = [ + migrations.AlterField( + model_name='teamarea', + name='camp', + field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='teamareas', to='camps.Camp'), + ), + ] diff --git a/src/teams/models.py b/src/teams/models.py index 3c7eb8f5..33e2a9bd 100644 --- a/src/teams/models.py +++ b/src/teams/models.py @@ -18,7 +18,7 @@ class TeamArea(CampRelatedModel): name = models.CharField(max_length=255) description = models.TextField(default='') - camp = models.ForeignKey('camps.Camp') + camp = models.ForeignKey('camps.Camp', related_name="teamareas", on_delete=models.PROTECT) responsible = models.ManyToManyField( 'auth.User', related_name='responsible_team_areas' diff --git a/src/utils/models.py b/src/utils/models.py index 5699b323..4c4ddd1b 100644 --- a/src/utils/models.py +++ b/src/utils/models.py @@ -20,7 +20,7 @@ class CleanedModel(models.Model): # which is not supposed to happen. Call them manually one by one instead. self.clean_fields() self.clean() - self.validate_unique() + self.validate_unique(exclude=None) except ValidationError as e: message = "Got ValidationError while saving: %s" % e if hasattr(self, 'request'):