If you are going to be the only one posting, it is probably safe for you to add the date to your post. This wont work if you have two posts with the same title on the same day, which for a small site is unlikly.
In both cases, you will need to have
CharField rather than
SlugField as / is not a
SlugField safe character when using the Django admin.
from django.db import models from django.template.defaultfilters import slugify import datetime class MyModel(models.Model): title = models.CharField(max_length=150, db_index=True) slug = models.Charfield(max_length=150, unique=True) .... .... def save(self): super(MyModel, self).save() date = datetime.date.today() self.slug = '%i/%i/%i/%s' % ( date.year, date.month, date.day, slugify(self.title) ) super(MyModel, self).save()
The second way, which you can also merge with the first add the id as the suffix.
/2009/01/30/1-the-slug.html or /1-the-slug.html
def save(self): super(MyModel, self).save() date = datetime.datetime.today() self.slug = '%i/%i/%i/%i-%s' % ( date.year, date.month, date.day, self.id, slugify(self.title) ) super(MyModel, self).save()
self.slug = '%i-%s' % ( self.id, slugify(self.title) )
You may need to change your URL matching Regular Expression. The following matches everything up to a dot. This wont work unless you have a file extensions, you could just match to the end of the string.
Note: Setting a Primary field yourself means id probably won't exist.