since you are using .first() in the query, it will return only 1 object so I don't think using a for loop our 'category' in template will work
On Mon, 18 Oct 2021 at 18:04, Katharine Wong <katharineo...@gmail.com> wrote: > Hi all, > > There is a question when I use distinct(). > When I try to use the ORM(distinct & value), I cannot get the value of the > attribute(foreign key). > > models.py > > class Category(models.Model): > category = models.CharField(max_length=20) > > class Article(models.Model): > category = models.ForeignKey(Category, on_delete=models.CASCADE) > > > views.py > category = > Article.objects.filter(visible=True).values('category').distinct().first() > return_result.update({'category': category}) > > template/xxx.html > > {% if category %} > {% for name in category %} > {{ name.category }} > ... > > the result is 4. (int) and there is no result if I type {{ > name.category.category }} > > I hope the result is Work which is the 4(category_id) refs. > > Thanks, > Kath > > -- > 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/CALUcQhHOQ7rcg_xLgJocyubWCN8pFOLmvrsnDqbkSWAWF-_Y2A%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CALUcQhHOQ7rcg_xLgJocyubWCN8pFOLmvrsnDqbkSWAWF-_Y2A%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/CAGp2JVEG%2Bk8fnseW4Tr_iME-dN_%3DTbojM%2BnL5kcwzFgyUdbjtQ%40mail.gmail.com.