Django数据库连接库问题

时间:2019-05-23 10:34:24

标签: python django python-3.x oracle pandas

我遇到了一个不合逻辑的问题。当我使用从cx_Oracle创建的连接时,它按假定的方式工作。但是当我使用Django DB connections时,它给出的结果却不是预期的。

import cx_Oracle
from django.db import connections
import pandas as pd

dsn_tns = cx_Oracle.makedsn('xx.x.xx.xxx', 'port', 'dbname')
cx_Oracle_conn = cx_Oracle.connect('user', 'pass', dsn_tns)

django_conn = connections["DB2"] # In django settings, I have a created "DB2" and passed the same parameters.

query = ''' 
SELECT (T1.ACCEPTED- T2.CANCELLED) AS "NET" FROM
(
SELECT ID, COUNT(1) AS ACCEPTED FROM ACCEPTED_TABLE
GROUP BY ID
) T1
LEFT JOIN
(SELECT ID, COUNT(1) AS CANCELLED FROM CANCELLED_TABLE
GROUP BY ID) T2
ON T1.ID=T2.ID
'''

df1 = pd.read_sql(query, cx_Oracle_conn) # Gives right result
df2 = pd.read_sql(query, django_conn) # Gives only count of T1.ACCEPTED(T2.CANCELLED having 0 count)

问题:

  1. 这是什么原因?连接如何影响查询的执行?
  2. 这是Django的错误吗?还是我需要以其他方式使用Django连接?
  3. django.db.backends.oracle如何工作?能详细解释吗?

版本:

Django == 2.1.2

Python == 3.6.7

'ENGINE': 'django.db.backends.oracle' # As mentioned in django document in DB connections

0 个答案:

没有答案
相关问题