gpt4 book ai didi

django - Django查询在哪里

转载 作者:行者123 更新时间:2023-12-03 11:13:55 26 4
gpt4 key购买 nike

如何使用django对象查询执行此操作:

 SELECT * FROM test WHERE (test_id IN (SELECT test_id FROM test_subject_set)) AND (test_begin_time < '') AND (test_end_time > '')

该模型:
class Test(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User)
groups = models.ManyToManyField(Group)


class TestSubjectSet(models.Model):
id = models.AutoField(primary_key=True)
test = models.ForeignKey(Test)

最佳答案

两种查询集是documented的一种实现方式。无论如何,它将是一个数据库命中。

test_ids = Subject.objects.all()
result = Test.objects.filter(test_id__in=test_ids).filter([some other filtering])

关于django - Django查询在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com