Vous pourriez également gérer cela depuis votre vue. Par exemple, si vous disposez d'une vue générique DetailView, vous pouvez remplacer l'option get_queryset et définir la méthode limace en minuscules :
models.py :
from django.db import models
from django.utils.text import slugify
class MyModel(models.Model):
"""MyModel
"""
# name attribute is used to generate slug
name = models.CharField(max_length=25, verbose_name='Name', unique=True)
slug = models.SlugField(unique=True, verbose_name='Slug')
# override save method to generate slug
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
self.slug = slugify(self.name)
super(MyModel, self).save(force_insert=False, force_update=force_update, using=using, update_fields=update_fields)
views.py :
from django.views import generic
class MyModelDetailView(generic.DetailView):
"""MyModelDetailView
Detail view of MyObject.
"""
model = MyModel
template_name = 'my_models/my_model_detail.html'
def get_queryset(self):
try:
self.kwargs['slug'] = self.kwargs['slug'].lower()
except Exception as err:
print(err)
pass
queryset = self.model.objects.filter()
return queryset
urls.py :
from . import views
from django.conf.urls import url
url(r'^my_model_detail/(?P<slug>[-\w]+)/$', views.MyModelDetailView.as_view(),
name='my_model_detail'),