|
2 | 2 |
|
3 | 3 | from django.db import models
|
4 | 4 | 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 |
6 | 6 | from django.utils.translation import ugettext as _
|
7 | 7 | from tastypie.models import create_api_key
|
8 | 8 |
|
@@ -184,12 +184,21 @@ def create_default_leagues(sender, instance, created, **kwargs):
|
184 | 184 |
|
185 | 185 |
|
186 | 186 | def set_union_by_district(sender, instance, **kwargs):
|
187 |
| - # Set according union if district is set |
| 187 | + # If district is set, set according union |
188 | 188 | if instance.district:
|
189 | 189 | instance.union = instance.district.union
|
190 | 190 |
|
191 | 191 |
|
| 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 | + |
192 | 200 | # Create API key for a new user
|
193 | 201 | post_save.connect(create_api_key, sender=User)
|
194 | 202 | post_save.connect(create_default_leagues, sender=District)
|
195 | 203 | pre_save.connect(set_union_by_district, sender=League)
|
| 204 | +m2m_changed.connect(set_club_by_team, sender=Team.players.through) |
0 commit comments