#11822 closed (invalid)
Slugify check for existing slugs in table
Reported by: | Alvin Mites | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | Keywords: | slug, slufigy | |
Cc: | josh@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Slugify would be more useful if it had an option to set the field name to check against for existing slugs with the same name
Change History (7)
comment:1 by , 15 years ago
Cc: | added |
---|
comment:2 by , 15 years ago
comment:3 by , 15 years ago
I suppose to make this work with slugify you would need to either pass 3 optional vars or a dict
unique[boolean], model, field
My ugly workaround for this designed for 1 model:
def unique_name(field_name, name):
clean_name = file_name(name)
test_name = clean_name
count = 1
while True:
dict = [field_name, test_name]
try:
tester = Photo.objects.get(dict)
except:
tester = False
if not tester:
return str(test_name)
else:
test_name = clean_name+str(count)
count = count + 1
tester = False
comment:4 by , 15 years ago
same ugly code, but wrapped to be readable
def unique_name(field_name, name): clean_name = file_name(name) test_name = clean_name count = 1 while True: dict = [field_name, test_name] try: tester = Photo.objects.get(dict) except: tester = False if not tester: return str(test_name) else: test_name = clean_name+str(count) count = count + 1 tester = False
comment:5 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
This would only work correctly if it could also save the data to the table. This is certainly beyond what a template filter ought to be doing -- that kind of logic belongs in your view functions or database - this is the reason SlugField exists. Therefore closing as INVALID.
comment:6 by , 15 years ago
I agree this is outside the scope of a tag. If you require this type of functionality, there are many third party scripts that will allow you to do this in a non-invasive way. For example http://www.djangosnippets.org/snippets/369/ is a pretty nice one. Use that instead amites.
comment:7 by , 12 years ago
Component: | ORM aggregation → Database layer (models, ORM) |
---|
I imagine this using an attribute like so:
How would this work? How would it know which model this was attached to? Remember, it's a string it takes.
If it encountered a duplicate, would it append a hyphen? a hyphen with a number?
How do you see this working amites?