M
M
MarsFM2016-08-06 22:26:48
Django
MarsFM, 2016-08-06 22:26:48

How to make cnc in django?

Can you tell me why it doesn't work, plz.
The first method is needed so that if the headers are the same, then id is added.
The second translates title into English.
If you do everything separately, then it will work.

def create_slug(instance, new_slug=None):
    slug = instance.title
    if new_slug is not None:
        slug = new_slug
    qs = Post.objects.filter(slug=slug).order_by("-id")
    exists = qs.exists()
    if exists:
        new_slug = "%s-%s" %(slug, qs.first().id)
        return create_slug(instance, new_slug=new_slug)
    return slug


def set_post_slug(sender, instance, *args, **kwargs):
    from pytils import translit
    instance.slug = translit.slugify(create_slug(instance))

pre_save.connect(set_post_slug, sender=Post)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abdulla Mursalov, 2016-08-06
@sportredwhite

def handle_post_slug(sender, instance, *args, **kwargs):
  from pyutils import translit
  slug = translit(instance.title)
  if Post.objects.filter(slug=slug).exclude(id=instance.id).exists():
    slug = "-".join([slug, str(instance.id)])
  instance.slug = slug

pre_save.connect(handle_post_slug, sender=Post)

Offhand. Can you elaborate on what "doesn't work"?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question