如何使用exclude和ForeignKey过滤查询集

时间:2017-07-04 01:51:13

标签: django python-2.7

我有两个型号

class Entry(models.Model):
    properties = here

class ReadedEntries(models.Model):
    entry = models.ForeignKey(Entry, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

如何从用户那里获取无readed条目?

我试试:

not_looked_videos = Entry.objects.exclude(ReadedEntries.objects.filter(user=request.user))

但我明白了:

ValueError: need more than 0 values to unpack

2 个答案:

答案 0 :(得分:1)

尝试使用isnull来设置所有空的ReadedEtries设置:

Entry.objects.filter(readedentries__isnull=True)

或者仅排除当前的readed条目: 用户

Entry.objects.exclude(readedentries__user=request.user))

答案 1 :(得分:1)

1,过滤~Q(id__in = ids)

其中ids = ReadedEntries.objects.filter(user = request.user).values_list('id',flat = True)

2.我仍在努力完成它......