我有疑问:
articles = Article.objects.filter(Q(owner=request.user)|Q(group__user=request.user))
但这会向我显示两篇文章(所有者=文章编辑组中的用户和用户)。
型号:
class ProfileGroup(models.Model):
user = models.ManyToManyField(user)
name = models.CharField(max_length=100)
class Article(models.Model):
title = models.CharField(max_length=200)
text = models.TextField()
group = models.ForeignKey(ProfileGroup)
owner = models.ForeignKey(user)
如何只获取非重复的文章?
答案 0 :(得分:4)
我会看一下不同的QS方法:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct
示例:
Article.objects.filter(whatever).distinct('id')