From 373a20aa44620db4032e64a2a781afd2f558efb4 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 9 Sep 2017 19:37:12 +0200 Subject: Génération automatique d’un slug pour les modèles Year et Timetable --- admin.py | 1 + models.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/admin.py b/admin.py index 2fc0e78..48930e0 100644 --- a/admin.py +++ b/admin.py @@ -21,6 +21,7 @@ from .models import Timetable, LastUpdate, Group, Room, Course, Year class YearAdmin(admin.ModelAdmin): prepopulated_fields = {"slug": ("name",)} list_display = ("name",) + ordering = ("name",) @admin.register(Timetable) diff --git a/models.py b/models.py index 6bb9733..061a213 100644 --- a/models.py +++ b/models.py @@ -21,20 +21,38 @@ from django.utils.text import slugify from .utils import parse_group -class Year(models.Model): +class SlugModel(models.Model): + def save(self): + if not self.slug: + self.slug = slugify(self.name) + + super(SlugModel, self).save() + + + class Meta: + abstract = True + + +class Year(SlugModel): name = models.CharField(max_length=16, verbose_name="année") slug = models.SlugField(max_length=16, unique=True, default="") def __str__(self): return self.name + def save(self): + if not self.slug: + self.slug = slugify(self.name) + + super(Year, self).save() + class Meta: verbose_name = "année" verbose_name_plural = "années" -class Timetable(models.Model): +class Timetable(SlugModel): year = models.ForeignKey(Year, on_delete=models.CASCADE, verbose_name="année") name = models.CharField(max_length=64, verbose_name="nom") url = models.URLField(max_length=255, verbose_name="URL") -- cgit v1.2.1