--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WYCuO9AMa7j4WxaCCZqdgE26YoUTksQptBSYtQ64K8O4g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BARzD-870cXr41yUpGQheNUUDHZxW1GVoR5QJR%3DiV259A4K3w%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WaFgP99TAJ%3D0-SSHcwdbfCJYn3x0EKk_b-cvyRRiBhDFQ%40mail.gmail.com.
I think I kinda understand what you want to achieve here. You want to use a slug field, but you are wondering what happens when two posts have the same slug name. Here is what I do. On your model.class Post(models.Model):title = models.CharField(max_length=50)slug = models.CharField(max_length=50)def _get_unique_slug(self):
slug = slugify(self.name)
unique_slug = slug
num = 1
while Category.objects.filter(slug=unique_slug).exists():
unique_slug = '{}-{}'.format(slug, num)
num += 1
return unique_slug
def save(self, *args, **kwargs):
if not self.slug:
self.slug = self._get_unique_slug()
super().save(*args, **kwargs)This way, no two posts can have the same slug field.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WYCuO9AMa7j4WxaCCZqdgE26YoUTksQptBSYtQ64K8O4g%40mail.gmail.com.
--
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADYqDX0QVJtizHHDN0FTfr1t7nf%2B32-5VgDk%3DJe9h9rQarUSQw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6Wa6Wqk8DSgObib8fk9vnFfJrV88pDbN8hEHvoWi9ZhEMA%40mail.gmail.com.