Capture Content Id

36 views
Skip to first unread message

Soumen Khatua

unread,
Jun 15, 2020, 7:03:35 AM6/15/20
to django...@googlegroups.com
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

Kayode Oladipo

unread,
Jun 15, 2020, 7:52:09 AM6/15/20
to django...@googlegroups.com
There's an id field built into every Django model (aka, the primary key (pk))

Just do, ModelInstance.id and that should do the trick.

Cheers.

--
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.

Soumen Khatua

unread,
Jun 15, 2020, 7:54:22 AM6/15/20
to django...@googlegroups.com
Could you give me an example??

Kelvin Sajere

unread,
Jun 15, 2020, 7:55:28 PM6/15/20
to django...@googlegroups.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.


If I get you, you wrote the detail view with a slug parameter instead of the regular pk or id parameter and you’d like to get the details of a post. Since you are using a slug parameter, just do ModelInstance.slug 

--
KeLLs

Kelvin Sajere

unread,
Jun 15, 2020, 7:55:28 PM6/15/20
to django...@googlegroups.com
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 <kells...@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.


--
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.




--
KeLLs

Kelvin Sajere

unread,
Jun 15, 2020, 7:55:50 PM6/15/20
to django...@googlegroups.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.

On Mon, Jun 15, 2020 at 08:03 Soumen Khatua <soumenk...@gmail.com> wrote:
--

Soumen Khatua

unread,
Jun 16, 2020, 1:00:23 AM6/16/20
to django...@googlegroups.com
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. 

Soumen Khatua

unread,
Jun 16, 2020, 1:00:38 AM6/16/20
to django...@googlegroups.com
Thank you for your response. 

Kelvin Sajere

unread,
Jun 16, 2020, 1:34:43 AM6/16/20
to django...@googlegroups.com
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPUw6Wa6Wqk8DSgObib8fk9vnFfJrV88pDbN8hEHvoWi9ZhEMA%40mail.gmail.com.

First, I noticed my code was wrong. That line should be Post.objects.fiter.  But then, I don’t understand what exactly you are trying to achieve. If your view is going to take the slug parameter, then your url must too.

--
KeLLs

Kelvin Sajere

unread,
Jun 16, 2020, 1:39:20 AM6/16/20
to django...@googlegroups.com
Another thing you should note is that my initial solution uses the filter to check if the slug already exist. This is actually the recommended way to query a database. This way, it really isn’t going take a toll on your application.
--
KeLLs
Reply all
Reply to author
Forward
0 new messages