fmartingr
/
shelfzilla
Archived
1
0
Fork 0

Model save auto generate slug if needed

This commit is contained in:
Felipe Martin 2014-04-04 12:08:47 +02:00
parent 3065a5c343
commit cdaedc48e2
1 changed files with 8 additions and 0 deletions

View File

@ -1,5 +1,6 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import slugify
class ReviewModel(models.Model):
@ -11,5 +12,12 @@ class ReviewModel(models.Model):
if hasattr(self, 'name'):
return self.name and self.name[0] or ''
def save(self, *args, **kwargs):
# If model have a name and slug attribute, save slug
# TODO set a model field to custom field name for slug creation
if not getattr(self, 'slug', None) and getattr(self, 'name', None):
self.slug = slugify(self.name)
return super(ReviewModel, self).save(*args, **kwargs)
class Meta:
abstract = True