本文介绍了Django查询在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用django对象查询:
How to do this using django object query:
SELECT * FROM test WHERE (test_id IN (SELECT test_id FROM test_subject_set)) AND (test_begin_time < '') AND (test_end_time > '')
模型: / p>
The model:
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)
推荐答案
两个查询器是这样做的方式。这将是一个数据库命中。
Two querysets is documented way of doing this. It will be one database hit anyway.
test_ids = Subject.objects.all()
result = Test.objects.filter(test_id__in=test_ids).filter([some other filtering])
这篇关于Django查询在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!