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

This commit is contained in:
Thomas Steen Rasmussen 2018-03-04 12:48:35 +01:00
parent 60c4bb49fb
commit fe4e47edb0
4 changed files with 30 additions and 2 deletions

View File

@ -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())

View File

@ -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'),
),
]

View File

@ -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'

View File

@ -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'):