【发布时间】:2019-02-21 21:00:10
【问题描述】:
正如标题所示,我有多组查询,每组都返回一个值列表。然后我使用值列表来过滤另一个查询集。目前,我一次只能执行第二步一个查询集。是否可以将我的初始值列表组合成一个超长列表?我正在尝试创建类似功能的活动/新闻提要。
views.py:
cookie_ids = Cookie.objects.filter(board__pk=self.kwargs['pk']).values_list('id',
flat=True)
sugar_ids = Sugar.objects.filter(board__pk=self.kwargs['pk']).values_list('id',
flat=True)
**then:
context['cookie_actions'] = Action.objects.filter(target_id__in=cookie_ids)
context['sugar_actions'] = Action.objects.filter(target_id__in=sugar_ids)
编辑:我认为这是唯一可能重要的模型
Models.py:
class Action(models.Model):
user = models.ForeignKey(User,
related_name='actions',
db_index=True)
verb = models.CharField(max_length=255)
target_ct = models.ForeignKey(ContentType,
blank=True,
null=True,
related_name='target_obj')
target_id = models.PositiveIntegerField(null=True,
blank=True,
db_index=True)
target = GenericForeignKey('target_ct', 'target_id')
created = models.DateTimeField(auto_now_add=True,
db_index=True)
class Meta:
ordering = ('-created',)
【问题讨论】:
-
请展示您的模型。
-
您使用的内容类型,仅通过target_id过滤会冲突。
-
@naqibhakimi 既然你提到了它,我就回去检查我认为已经在工作的东西(仅使用 cookie_ids),并且有数据显示在其他板上。所以当你说冲突时,你是什么意思?知道为什么会这样吗?
标签: django django-models django-queryset