The QuerySet value for an exact lookup must be limited to one result using slicing-Django

前端 未结 3 1999
星月不相逢
星月不相逢 2021-01-13 00:37

I\'m building a news website.While I tried to get the list of relative news which have the same tags.The error said:The QuerySet value for an exact lookup must be li

3条回答
  •  被撕碎了的回忆
    2021-01-13 01:18

    The following will work:

    def newsDetailView(request, news_pk):
        news = get_object_or_404(News, id=news_pk)
        relative_news = News.objects.filter(tag__id__in=news.tag.all())
    

提交回复
热议问题