Yeah but in this way the time complexity will be very high, just think if I'll have more than 1 million post than it will check all the field if it is available or not. However my query is different I want to fetch the data based on ID only but I want to make url as slug name, so it means the function will take slug as a parameter not ID.
On Tue 16 Jun, 2020, 1:24 AM Kelvin Sajere, <kells4r...@gmail.com> wrote: > I forgot that you need to import this > > from django.utils.text import slugify > > On Mon, Jun 15, 2020 at 6:35 PM Kelvin Sajere <kells4r...@gmail.com> > wrote: > >> 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. >> >> >> On Mon, Jun 15, 2020 at 08:03 Soumen Khatua <soumenkhatua...@gmail.com> >> wrote: >> >>> Hi Folks, >>> >>> I have one Blog post project here user can see the blog and by clicking >>> on the blog title they can see the details of the blog, so here Slug will >>> be working here as a URL,but in the details view, How I can fetch that >>> particular content details without having the ID because slug/title is the >>> parameter of the view and it can be duplicated. >>> Any help will be appreciated >>> >>> Thank You >>> >>> Regards, >>> Soumen >>> >>> -- >>> 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+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/CAPUw6WYCuO9AMa7j4WxaCCZqdgE26YoUTksQptBSYtQ64K8O4g%40mail.gmail.com >>> <https://groups.google.com/d/msgid/django-users/CAPUw6WYCuO9AMa7j4WxaCCZqdgE26YoUTksQptBSYtQ64K8O4g%40mail.gmail.com?utm_medium=email&utm_source=footer> >>> . >>> >> >> >> > > -- > KeLLs > > -- > 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+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CADYqDX0QVJtizHHDN0FTfr1t7nf%2B32-5VgDk%3DJe9h9rQarUSQw%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CADYqDX0QVJtizHHDN0FTfr1t7nf%2B32-5VgDk%3DJe9h9rQarUSQw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6WZG3ZdSiB3xL6soM7MTL13U75ffgRNzOpLBDOu0BPfmYw%40mail.gmail.com.