简体   繁体   中英

create unique slug django

I have a problem with creating unique slugs using django models. I want to allow the admin user to change the slug from the edit page in the admin. When a slug already exists there should be "slug + _1", "slug + _2" etc. Also when a new page is created and there is no slug given the slug should be the page title. I have this code but for some reason the admin keeps saying "Page with this Slug already exists." when I add a page with a slug that already exists. Hope somebody can help me

def save(self, *args, **kwargs):
    if not self.id and not self.slug:
        self.slug = slugify(self.page_title)

    else:
        self.slug = slugify(self.slug)

    slug_exists = True
    counter = 1
    slug = self.slug
    while slug_exists:
        try:
            slug_exits = Page.objects.get(slug=slug)
            if slug_exits == slug:
                slug = self.slug + '_' + str(counter)
                counter += 1
        except:
            self.slug = slug
            break
    super(Page, self).save(*args, **kwargs)

Try this. Didn't test it myself. But it should give you the idea.

import re
def save(self, *args, **kwargs):
    if not self.id: # Create
        if not self.slug: # slug is blank
            self.slug = slugify(self.page_title)
        else: # slug is not blank
            self.slug = slugify(self.slug)
    else: # Update
        self.slug = slugify(self.slug)

    qsSimilarName = Page.objects.filter(slug__startswith='self.slug')

    if qsSimilarName.count() > 0:
        seqs = []
        for qs in qsSimilarName:
            seq = re.findall(r'{0:s}_(\d+)'.format(self.slug), qs.slug)
            if seq: seqs.append(int(seq[0]))

        if seqs: self.slug = '{0:s}_{1:d}'.format(self.slug, max(seqs)+1)

    super(Page, self).save(*args, **kwargs)

Three problems in your code.

  1. The first else means either self.id or self.slug is NOT blank. So if self.id is NOT blank and self.slug is blank, self.slug will not get a value.
  2. slug_exits == slug will always be False, because slug_exits is a Model object and slug is a string. This is why you get the error!
  3. You did a query in the loop, which may cause lots of hits to the DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM