diff --git a/pandora/event/models.py b/pandora/event/models.py index a378c4c3e..f7d1baaa7 100644 --- a/pandora/event/models.py +++ b/pandora/event/models.py @@ -4,7 +4,7 @@ from __future__ import division, with_statement import re -from django.db import models +from django.db import models, transaction from django.contrib.auth.models import User from django.db.models import Q import ox @@ -85,9 +85,11 @@ class Event(models.Model): matches = [-1] return Annotation.objects.filter(id__in=matches) + + @transaction.commit_on_success def update_matches(self): matches = self.get_matches() - self.matches = matches.count() + numberofmatches = matches.count() for i in self.annotations.exclude(id__in=matches): self.annotations.remove(i) for i in matches.exclude(id__in=self.annotations.all()): @@ -98,7 +100,8 @@ class Event(models.Model): for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()): self.items.add(i) #only update matches, other values might have been changed - Event.objects.filter(id=self.id).update(matches=self.matches) + if self.matches != numberofmatches: + Event.objects.filter(id=self.id).update(matches=numberofmatches) def set_name_sort(self, value=None): if not value: diff --git a/pandora/place/models.py b/pandora/place/models.py index 9c01b2eec..31ed45f40 100644 --- a/pandora/place/models.py +++ b/pandora/place/models.py @@ -4,7 +4,7 @@ from __future__ import division, with_statement import re -from django.db import models +from django.db import models, transaction from django.contrib.auth.models import User, Group from django.db.models import Q import ox @@ -94,9 +94,10 @@ class Place(models.Model): matches = [-1] return Annotation.objects.filter(id__in=matches) + @transaction.commit_on_success def update_matches(self): matches = self.get_matches() - self.matches = matches.count() + numberofmatches = matches.count() for i in self.annotations.exclude(id__in=matches): self.annotations.remove(i) for i in matches.exclude(id__in=self.annotations.all()): @@ -106,7 +107,8 @@ class Place(models.Model): self.items.remove(i) for i in Item.objects.filter(id__in=ids).exclude(id__in=self.items.all()): self.items.add(i) - Place.objects.filter(id=self.id).update(matches=self.matches) + if self.matches != numberofmatches: + Place.objects.filter(id=self.id).update(matches=numberofmatches) def save(self, *args, **kwargs): if not self.name_sort: