过滤列表,没有结果。代码和里面的更多细节

时间:2014-06-04 15:53:42

标签: django list django-queryset

首先,我使用articles_in_cat(works)获取类别中的文章,然后尝试使用articles_hit获取代表这些文章的HitCount对象。 articles_hit不会抛出任何错误,但它是一个空列表。知道问题是什么吗?

def cat_home(request, id):
    n = categories.objects.get(id = id)
    articles_in_cat = list(article.objects.filter(category = n))
    articles_hit = list(HitCount.objects.filter(object_pk = articles_in_cat).order_by('hits')

1 个答案:

答案 0 :(得分:0)

让我们再试一次:

n = categories.objects.get(id = id) #  you say it works

# you wrote 'article' but model's name should be Article
articles_titles_in_cat [a.title for a in article.objects.filter(category = n)] 
# it should give you articles' title which match category n

#and then
articles_hit = list(HitCount.objects.filter(object_pk__in = articles_titles_in_cat).order_by('hits'))
#assuming object_pk contains "article's title" as you mentioned in a coment