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 > '')
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)
In Django, we can use the id__in query with a queryset to filter down a queryset based on a list of IDs. However, by default this will fail if your IDs are UUIDs.
To filter a Python Django query with a list of values, we can use the filter method with in . to search Blog entries with pk set to 1,4 or 7 by calling Blog. objects. filter with the pk_in argument set to [1, 4, 7] .
In the Django QuerySet API, F() expressions are used to refer to model field values directly in the database.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With