0

我有疑问:

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)

如何仅获取非重复文章?

4

1 回答 1

4

我会看看不同的QS方法:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct

例子:

Article.objects.filter(whatever).distinct('id')
于 2013-05-15T13:05:15.860 回答