Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法把它转换成django orm?_Django_Postgresql_Orm - Fatal编程技术网

有没有办法把它转换成django orm?

有没有办法把它转换成django orm?,django,postgresql,orm,Django,Postgresql,Orm,我需要获取表1中的所有行,即使它在表2中不存在,并将其显示为零。我使用rawsql查询得到它,但在django ORM中我得到的值仅存在于表2中。django orm上唯一的区别在于我使用的是内部联接,而在原始sql查询中我使用的是左联接。有什么方法可以实现这一点,或者我应该使用原始sql查询吗?谢谢 Django ORM: total=ApplicantInfo.objects.select_related('source_type').values('source_type__source_

我需要获取
表1
中的所有行,即使它在
表2中不存在,并将其显示为零。我使用
rawsql查询得到它
,但在django ORM中我得到的值仅存在于
表2中
。django orm上唯一的区别在于我使用的是内部联接,而在原始sql查询中我使用的是左联接。有什么方法可以实现这一点,或者我应该使用原始sql查询吗?谢谢

Django ORM:

total=ApplicantInfo.objects.select_related('source_type').values('source_type__source_type').annotate(total_count=Count('source_type'))
原始SQL中DJANGO ORM的输出:

SELECT "applicant_sourcetype"."source_type", COUNT("applicant_applicantinfo"."source_type_id") AS "total_count" FROM "applicant_applicantinfo" INNER JOIN "applicant_sourcetype" ON ("applicant_applicantinfo"."source_type_id" = "applicant_sourcetype"."id") GROUP BY "applicant_sourcetype"."source_type"
SELECT source.source_type, count(info.source_type_id) as total_counts from applicant_sourcetype as source LEFT JOIN applicant_applicantinfo as info ON source.id = info.source_type_id GROUP BY source.id
原始SQL:

SELECT "applicant_sourcetype"."source_type", COUNT("applicant_applicantinfo"."source_type_id") AS "total_count" FROM "applicant_applicantinfo" INNER JOIN "applicant_sourcetype" ON ("applicant_applicantinfo"."source_type_id" = "applicant_sourcetype"."id") GROUP BY "applicant_sourcetype"."source_type"
SELECT source.source_type, count(info.source_type_id) as total_counts from applicant_sourcetype as source LEFT JOIN applicant_applicantinfo as info ON source.id = info.source_type_id GROUP BY source.id

如果要使用左联接,则无法查询
applicationinfo
。改为查询
SourceType

qs = (SourceType.objects
    .values('source_type')
    .annotate(cnt=Count('applicantinfo'))
    .values('source_type', 'cnt')
)

由于您尚未发布模型,您可能需要调整字段名称以使其正常工作。

如果您想要左连接,则无法查询
applicationinfo
。改为查询
SourceType

qs = (SourceType.objects
    .values('source_type')
    .annotate(cnt=Count('applicantinfo'))
    .values('source_type', 'cnt')
)
由于您尚未发布模型,因此可能需要调整字段名称以使其正常工作