Opened 6 years ago

Closed 6 years ago

Last modified 2 years ago

#11822 closed (invalid)

Slugify check for existing slugs in table

Reported by: amites 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: UI/UX:

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 Changed 6 years ago by joshuajonah

  • Cc josh@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by joshuajonah

I imagine this using an attribute like so:

slugify('some string', unique=True)

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?

comment:3 Changed 6 years ago by amites

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 Changed 6 years ago by amites

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 Changed 6 years ago by lukeplant

  • Resolution set to invalid
  • Status changed from new to 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 Changed 6 years ago by joshuajonah

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 Changed 2 years ago by akaariai

  • Component changed from ORM aggregation to Database layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top