【问题标题】:how to display articles by user in django?如何在 django 中按用户显示文章?
【发布时间】:2015-08-10 01:18:48
【问题描述】:

我有文章模型:

class Article(models.Model):
    title = models.CharField(max_length=200)
    body = models.TextField()
    pub_date = models.DateTimeField(editable=False)
    modified = models.DateTimeField(editable=True)    
    whopost = models.ForeignKey(UserProfile)

我有用户配置文件模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    info1 = models.BooleanField(default=False)
    mobile = models.CharField(max_length=50,default="")

我尝试在用户和文章之间建立关系,以便知道谁创建了文章。这将允许我按登录用户显示文章。

所以,

我尝试在我的文章视图中关注:

def articles(request):

    args = {}
    args.update(csrf(request))

    user_articles = Article.objects.filter(whopost_id=request.user.id)
    latest_articles = Article.objects.order_by('-pub_date')[:10]

    #display articles in order by date
    #args['articles'] = latest_articles

    #display articles in order by user
    args['articles'] = user_articles

    args['language'] = language
    args['session_language'] = session_language

    return render(request, 'articles.html', args)

我创建了三个用户:

用户 A 用户 B 用户C

在我的 userprofile_userprofile 中有以下用户

id | user_id
1  | 1
2  | 13
3  | 14

在我的 auth_user 用户中有以下用户

id 
1
13
14

所以,我得到的值是 1、2、3 而不是 1、13、14。

我应该在我的代码中更改什么以获得值 1, 13, 14 而不是 1, 2, 3 ?

另外,问题的第二部分是:

如何按用户按日期显示文章?

【问题讨论】:

    标签: django views models


    【解决方案1】:

    问题的第一部分:

    要获取userprofile ID 而不是user ID,只需访问您与UserProfile 的一对一关系,如下所示:

    userprofile_id = request.user.userprofile.id
    

    问题的第二部分:

    要按日期订购特定用户的文章,您只需执行以下操作:

    articles_by_date = Article.objects.filter(
        whopost__id=request.user.userprofile.id
    ).order_by('-pub_date')
    

    【讨论】:

    • 回答您的第一个问题:是的,我在 whopost_id 和 whopost__id 中都获得了相同的功能。我已按照您的建议使用 userproile_id = request.user.userprofile.id 并修改了过滤器,如下所示:“user_articles = Article.objects.filter(whopost__id = request.user.userprofile.id)”并且它有效!虽然,我仍然不确定我应该做 _id 还是 __id。
    • 根据docs 单下划线仅适用于ForeignKeys。对于所有其他查找,您必须使用双下划线。哇,我今天学到了一些东西:)
    • 所以,听起来我应该使用单下划线,因为 whopost=models.ForeignKey(UserProfile)。对吗?
    • 是的,这是正确的。您正在此处查找ForeignKey (whopost_id=...),因此它适用于单个下划线。如果 whopost 是其他内容(例如 ManyToManyField),则必须使用双下划线(whopost__id=...)。我希望现在很清楚 :)
    • 感谢您对我的问题的所有帮助。我决定使用罗杰·费德的回答作为最有帮助的,只是因为他是第一个。还是谢谢你!
    【解决方案2】:

    在查询集中使用 values() 命令。

    user_articles = Article.objects.filter(whopost=request.user.userprofile.id).values('whopost').order_by('-pub_date')
    

    【讨论】:

    • 我得到一个 AttributeError "'QuerySet' 对象没有属性 'fields'"
    • 现在不显示文章的“标题”和“正文”。请看上传的图片:i.imgur.com/dlVhVRJ.png
    • 您可以在 values() 命令中添加要显示的字段。这样,您只显示您需要的内容。例如。值('whopost'、'title'、'body')等等。
    • (whopost=request.user.useprofile.id).values() 与 (whopost__id = request.user.useprofile.id) 有何不同?我不知道是否应该使用 whopost 或 whopost__id 或 whopost_id 访问它。我怎么知道?
    • whopost__id 仅比较 id。仅使用 whopost 比较对象的实例。它将节省一个额外的调用来检查 whopost 的 id 以进行比较
    猜你喜欢
    • 2023-03-03
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 2023-02-02
    • 2023-02-25
    • 1970-01-01
    相关资源
    最近更新 更多