suppose I have the following three models, see as follow: I wanna to construct a Django model query to archive the same effect as the following SQL statement.
SQL statement
select B.value, C.special
from B inner join C
where B.version = C.version and B.order = C.order;
I got the following three models:
class Process(models.Model):
name = models.CharField(max_length=30)
description = models.CharField(max_length=150)
class ProcessStep(models.Model):
process = models.ForeignKey(Process)
name = models.CharField(max_length=30)
...
order = models.SmallIntegerField(default=1)
version = models.SmallIntegerField(null=True)
class Approve(models.Model):
process = models.ForeignKey(Process)
content = models.CharField(max_length=300)
...
version = models.SmallIntegerField(null=True)
order = models.SmallIntegerField(default=0)
I want to find all Approves that have the same (version, order) tuple matching against the ProcessStep model.
像这样吗
Approve.objects.filter(process__processstep_set__order=<value>)
Right now I can only come out with a solution by using raw SQL, see the following code.
from django.db import connection
cursor = connection.cursor()
cursor.execute("
SELECT A.id, B.id
from approval_approve as A inner join approval_approvalstep as B
where A.process_id = B.process.id
and A.order = B.order
and A.version = B.version;")
# do something with the returned data
If you guys got any better solution, I would like to hear from you, thanks~
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.