I have seen a variety of different methods for generating unique slugs: Ex. 1, Ex.2, Ex. 3, Ex. 4, etc. etc.
I want to create unique slugs upon saving a ModelForm. If my models are like:
class Phone(models.Model):
user = models.ForeignKey(User)
slug = models.SlugField(max_length=70, unique=True)
year = models.IntegerField()
model = models.ForeignKey('Model')
series = models.ForeignKey('Series')
Say that the Phone
object has the following values (via submitted ModelForm):
Phone.user = dude
Phone.year = 2008
Phone.model = iphone
Phone.series = 4S
I want the url for this object to appear like:
http://www.mysite.com/phones/dude-2008-iphone-4S
I understand that I should use slugify
via either signals or over-riding the save method to make this happen. But if user dude
creates a second 2008 iphone 4S object, what the best way to create a unique slug for this object? I want the additional objects's url to look like:
http://www.mysite.com/phones/dude-2008-iphone-4S-2
and
http://www.mysite.com/phones/dude-2008-iphone-4S-3
http://www.mysite.com/phones/dude-2008-iphone-4S-4
#...etc ...
After googling, it seems like there are a variety of different methods for creating slugs in django, which has been confusing when trying to figure out best practices.
Thanks a lot for any advice and clarification on this issue!