avi*_*ldg 4 python database django django-models
如何author从一组“ authors”(Users)中过滤带有“ ”的对象?
“对象”是Posts,有一个author( ForeignKeyto User)。
我对此非常困惑,所以我很感激它的帮助。当然,人们可以通过手动过滤它们的天真方式来解决这个问题,但这会严重打击数据库。不管怎么说,还是要谢谢你。
编辑: 帖子列表:
class Post(models.Model):
'''A Post or a Status Update.
'''
content=models.CharField(max_length=200)
author=models.ForeignKey(django.contrib.auth.models.User, related_name="author")
tags=models.ManyToManyField(Tag)
replyTo=models.ManyToManyField(django.contrib.auth.models.User, related_name="replyTo")
# Snip model methods
Run Code Online (Sandbox Code Playgroud)
澄清:我试图根据一组用户而不是单个用户进行过滤(这很容易做到) when=models.DateTimeField(auto_now=True)
感谢所有帮助解决上一个问题的人。现在我要问最后一件事:
来自 UserProfile 的代码摘录(连接到用户):
def get_updates():
return Post.objects.filter(author__in=(list(self.friends.all()) + [self]))
Run Code Online (Sandbox Code Playgroud)
这是获取作者及其朋友所有帖子的最有效方法吗?(注意:这是一个幼稚的实现,因为它不处理分页等。稍后会这样做)
就像是:
Post.objects.filter(author=user)
Run Code Online (Sandbox Code Playgroud)
user相关用户应该在哪里工作,但没有模型很难给出好的答案
编辑
现在我明白了你的问题,试试这个:
Post.objects.filter(author__in=users)
Run Code Online (Sandbox Code Playgroud)
其中 users 是用户的集合
| 归档时间: |
|
| 查看次数: |
7554 次 |
| 最近记录: |