Django查询以获取用户最喜欢的帖子? [英] Django query to get User's favorite posts?

查看:71
本文介绍了Django查询以获取用户最喜欢的帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Django术语感到有些困惑。所以我有3个模型:Post,UserProfile(User),Favorite。收藏夹跟踪用户喜欢的帖子。

I'm a little confused by the Django lingo. So I have 3 models: Post, UserProfile(User), Favorite. Favorite keeps track of which Posts a User has favorited.

发布后->收藏夹<---用户/用户个人资料

Post--->Favorite<---User/UserProfile

收藏夹模型:

class Favorite(models.Model):
    user = models.ForeignKey(User, unique=False)
    post = models.ForeignKey(Post, unique=False)

    def __unicode__(self):
        return self.user.username

UserProfile模型:

UserProfile model:

class UserProfile(models.Model) :
    user = models.ForeignKey(User, unique=True)

    def get_favorites(self):
        if self.user:
            return self.user.favorite_set.all()

在我的post_list视图中,我将所有帖子传递给模板,并且在模板中有一个for显示所有帖子的循环。

In my post_list view I pass all Posts to my template, and in the template I have a for loop that displays all Posts.

{% for post in post_list %}
<hr/>
<div id="post_{{ post.id }}">
    {% include 'posts/_post.html' %}
</div>
{% endfor %}

现在在该for循环中,我想提出一个逻辑会显示收藏!登录用户是否喜欢该帖子。我认为常规SQL是这样的:

Now in that for loop I would like to put a logic that will display "Favorited!" if the logged-in User has favorited the Post. I think the conventional SQL is something like this:

SELECT favorite.post FROM favorite WHERE favorite.user = user.id

因此,在模板循环中我可以这样做

So that in the template loop I can do

{% if post in the.above.SQL.query%}Favorited!{% endif %}

现在由于某种原因我不能将其翻译为Django lingo。非常感谢您的帮助!

Now I just can't translate that to Django lingo for some reason. Your help is much appreciated!

推荐答案

要认识的是,您的收藏夹模型实际上是Post和User之间的多对多关系的穿透表。如果您在某个地方声明ManyToManyField,Django实际上可以自动管理它。就个人而言,我会在UserProfile上执行此操作-这样关系实际上就变成了Post和UserProfile之间的关系:

The thing to recognise is that your Favorite model is actually the through table of a many-to-many relationship between Post and User. Django can actually manage that automatically if you declare a ManyToManyField somewhere. Personally, I would do that on UserProfile - so the relationship actually becomes one between Post and UserProfile:

class UserProfile(models.Model):
    favorites = models.ManyToManyField(Post, related_name='favorited_by')

现在您不需要 get_favorites 方法,因为该方法可通过 userprofile.favorites.all()使用。您可以照原样使用此模板:

Now you don't need your get_favorites method, as it is available via userprofile.favorites.all(). You could just use this as-is in the template:

{% if post in userprofile.favorites.all %}Favorited!{% endif %}

但这会导致效率极低,因为您将对帖子列表中的每个帖子进行相同的查询。因此,使用 {%with%} 标记可在循环之前获取收藏夹一次:

but this will end up being extremely inefficient, as you'll be doing the same identical query for each post in your list of posts. So, use the {% with %} tag to get the favorites once before the loop:

{% with userprofile.favorites.all as favorite_posts %}
  {% for post in post_list %}
    {% if post in favorite_posts %}Favorited{% endif %}
    ...
  {% endfor %}
{% endwith %}

这篇关于Django查询以获取用户最喜欢的帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆