Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try</p> <pre><code>user.following.filter(to_user__status=1) </code></pre> <p>the <code>user.following</code> is still querying on <code>User</code>, thus you need to span relation w/ <code>__</code> to <code>Follow</code> here.</p> <p>The two fields here, <code>from_user</code> and <code>to_user</code>, are both <code>ForeignKey</code> pointing to <code>User</code> model. Thus for an <code>User()</code> instance <code>u</code>:</p> <ul> <li><code>u.following</code> searches for the <code>User()</code>s who have relationship w/ <code>u</code> through the intermediate table, the <code>Follow</code>. The key here is that <code>u.following</code> picks the first <code>ForeignKey</code> in the <code>Follow</code> that points to the <code>User</code>, as the reference to <code>u</code> itself. Thus for your version of <code>Follow</code>, <code>u.following.filter(to_user__status=1)</code> filters on the <code>Follow</code> items having <code>from_user</code> equals to <code>u</code> and <code>to_user</code> w/ <code>status</code> equals to <code>1</code>. The lookup is typical <a href="https://docs.djangoproject.com/en/1.4/topics/db/queries/#following-relationships-backward" rel="nofollow">following relationship backwards</a> </li> <li><code>u.from_user</code> searches the intermediate table for those having <code>from_user</code> equals to <code>u</code></li> <li><code>u.to_user</code> searches the intermediate table for those having <code>to_user</code> equals to <code>u</code></li> </ul> <p>Also, you could filter on the <code>ForeignKey</code> directly, w/ remembering that the <code>from_user</code> and <code>to_user</code> are both ref the <code>Follow</code>:</p> <pre><code>User.objects.filter(to_user__from_user=user, to_user__status=1) # user as from_user User.objects.filter(from_user__to_user=user, from_user__status=1) # user as to_user User.objects.filter(following=user) # those who are followed by `to_user` user </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload