Use natural foreign keys to refer associations

This commit is contained in:
Martin Pépin 2017-08-11 14:56:41 +01:00
parent 10543341b7
commit 714e702af7
3 changed files with 14 additions and 3 deletions

View file

@ -26,7 +26,7 @@
"registration_open": true,
"old": false,
"associations": [
"COF"
["COF"]
]
}
},
@ -43,7 +43,7 @@
"registration_open": true,
"old": false,
"associations": [
"BDS"
["BDS"]
]
}
},

View file

@ -86,7 +86,7 @@ class Migration(migrations.Migration):
name='Association',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30, verbose_name="nom de l'association")),
('name', models.CharField(max_length=30, unique=True, verbose_name="nom de l'association")),
('members_group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='member_group_of', to='auth.Group', verbose_name='groupe des membres')),
('staff_group', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='staff_group_of', to='auth.Group', verbose_name='groupe des membres du bureau')),
],

View file

@ -66,9 +66,17 @@ def post_delete_user(sender, instance, *args, **kwargs):
instance.user.delete()
class AssociationManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
class Association(models.Model):
objects = AssociationManager()
name = models.CharField(
_("nom de l'association"),
unique=True,
max_length=30
)
staff_group = models.ForeignKey(
@ -86,6 +94,9 @@ class Association(models.Model):
verbose_name=_("groupe des membres"),
)
def natural_key(self):
return self.name
class Meta:
verbose_name = _("association")
verbose_name_plural = _("associations")