Skip to content

Commit ef905be

Browse files
committed
When a player is added to a team, also add him to the respective club
1 parent 7ed500f commit ef905be

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Diff for: models.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.db import models
44
from django.contrib.auth.models import User
5-
from django.db.models.signals import post_save, pre_save
5+
from django.db.models.signals import post_save, pre_save, m2m_changed
66
from django.utils.translation import ugettext as _
77
from tastypie.models import create_api_key
88

@@ -184,12 +184,21 @@ def create_default_leagues(sender, instance, created, **kwargs):
184184

185185

186186
def set_union_by_district(sender, instance, **kwargs):
187-
# Set according union if district is set
187+
# If district is set, set according union
188188
if instance.district:
189189
instance.union = instance.district.union
190190

191191

192+
def set_club_by_team(sender, instance, action, reverse, model, pk_set, **kwargs):
193+
if action == 'post_add':
194+
for pk in pk_set:
195+
player = model.objects.get(pk=pk)
196+
player.clubs.add(instance.club)
197+
player.save()
198+
199+
192200
# Create API key for a new user
193201
post_save.connect(create_api_key, sender=User)
194202
post_save.connect(create_default_leagues, sender=District)
195203
pre_save.connect(set_union_by_district, sender=League)
204+
m2m_changed.connect(set_club_by_team, sender=Team.players.through)

0 commit comments

Comments
 (0)