开发者

Django query filter a set of data

开发者 https://www.devze.com 2023-01-03 18:57 出处:网络
if a have a query like following = Relations.objects.filter(开发者_StackOverflow中文版initiated_by = request.user)

if a have a query like

    following = Relations.objects.filter(开发者_StackOverflow中文版initiated_by = request.user)

in which i'm having all the users followed by the currently logged in user, and i want to display those user's blog posts. Using a query like:

    blog = New.objects.filter(created_by = following)

it only shows me the blog posts of the user with the id = 1 (though the currently logged in user doesn't actually follow him) in template i have :

{% for object in blog %}
<a href='/accounts/profile_view/{{object.created_by}}/'> {{object.created_by}}</a> <br /> 
{{object.post}}<br />
{% endfor %}

Where am i wrong?


.filter() returns a collection, not an occurence. Thus, I'd say problem is that second query should be

blog = New.objects.filter(created_by__in = following)


Or even simpler :

bloc = New.objects.filter(created_by__initiated_by = request.user)

But that feels strange to me... are you sure of your model design ?

0

精彩评论

暂无评论...
验证码 换一张
取 消