Code

Changes between Version 4 and Version 5 of SlugifyUniquely


Ignore:
Timestamp:
12/20/10 05:01:21 (3 years ago)
Author:
Loreana
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • SlugifyUniquely

    v4 v5  
    1 It's common for me to want a unique slug based on a user supplied name of an object. Following the pattern outlined at [http://www.b-list.org/weblog/2006/11/02/django-tips-auto-populated-fields djang-tips-auto-populated-fields], it's easy to get the field populated without user intervention. However, by setting the slug field in the save() method the field isn't validated and thus unique=True restrictions aren't checked. I wrote the following code to attempt to pick slugs which are unique within a particular model. It's certainly not ideal (it suffers from race condition as well as potential for pounding a database with lots of small queries until it finds a good slug field), but it works for my small/mid-sized apps. 
     1It's common for me to want a unique slug based on a user supplied name of an object. Following the pattern outlined at [http://www.b-list.org/weblog/2006/11/02/django-tips-auto-populated-fields djang-tips-auto-populated-fields] and some useful instructions from [http://www.superiorpapers.com research papers], it's easy to get the field populated without user intervention. However, by setting the slug field in the save() method the field isn't validated and thus unique=True restrictions aren't checked. I wrote the following code to attempt to pick slugs which are unique within a particular model. It's certainly not ideal (it suffers from race condition as well as potential for pounding a database with lots of small queries until it finds a good slug field), but it works for my small/mid-sized apps. 
    22 
    33Feel free to suggest improvements to 'wam-djangowiki spam@wamber.net' (remove the ' spam' from the address there).