arm架构,django4.2.7适配达梦8数据库

【Python相关包版本信息】

Django 4.2.7

django-dmPython 3.1.7

dmPython 2.5.5

【达梦数据库版本】

DM Database Server 64 V8

DB Version: 0x7000c

适配过程中发现的问题如下:

错误一:django.core.exceptions.ImproperlyConfigured: 'django_dmPython' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of: 'mysql', 'oracle', 'postgresql', 'sqlite3'

解决方法:将django_dmPython和django_dmPython-3.1.7.dist-info复制一份,放到django下面的db/backends/目录下,因此后面发现与此包相关的问题,需要修改2处。

错误二-1:ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/usr/local/python/lib/python3.12/site-packages/django/utils/encoding.py). Did you mean: 'force_bytes'?

修改django/db/backends/django_dmPython/base.py,将18行中的force_text去掉,因为在代码中没有被调用。

###你也可以在django/utils/encoding.py文件中添加此函数,这样修改后,就可以忽略错误二-2、错误二-3。

老版本django中的force_text函数的定义如下:

def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):

warnings.warn(

'force_text() is deprecated in favor of force_str().',

RemovedInDjango40Warning, stacklevel=2,

)

return force_str(s, encoding, strings_only, errors)

错误二-2:ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/usr/local/python/lib/python3.12/site-packages/django/utils/encoding.py). Did you mean: 'force_bytes'?

修改django/db/backends/django_dmPython/operations.py文件,将前面的import导入的force_text去掉,同时将下面的调用修改为force_str(2处)。

错误二-3:ImportError: cannot import name 'force_text' from 'django.utils.encoding' (/usr/local/python/lib/python3.12/site-packages/django/utils/encoding.py). Did you mean: 'force_bytes'?

修改django/db/backends/django_dmPython/utils.py文件,将前面的import导入的force_text去掉,同时将下面的调用修改为force_str(1处)

错误三、ImportError: cannot import name 'Random' from 'django.db.models.expressions' (/usr/local/python/lib/python3.12/site-packages/django/db/models/expressions.py)

老版本django中的Random类的定义如下:

class Random(Expression):

output_field = fields.FloatField()

def repr(self):

return "Random()"

def as_sql(self, compiler, connection):

return connection.ops.random_function_sql(), []

修改方法:修改django/db/models/expressions.py文件,在代码最后面,增加上述定义。(不要尝试将其加入到django_dmPython/compiler.py,因为上面这个类又调用了Expression,比较麻烦。)

错误四:django.core.exceptions.FullResultSet

解决方法:修改django/db/models/sql/where.py,将175和176行注释掉(这样修改肯定不对,不过目前暂未发现影响,后期发现了再改)。

错误五:AttributeError: 'Query' object has no attribute 'explain_query'

解决方法:修改django_dmPython/compiler.py,将300-304行注释掉(这样修改肯定不对,不过目前暂未发现影响,后期发现了再改,不过应该影响不大,这个应该是生成explain查询sql语句性能的动作)。

老版本/django/db/models/sql/query.py代码中定义了explain_query变量,在Query类中的__init__函数中定义此变量,赋默认值为False;在函数explain中将其值修改为了explain了。

错误六:Invalid column name [AAAAAAAAAAAAAAAAAC]

这个问题是达梦驱动的问题,直接按照下面修改就行。

解决方法:修改django_dmPython/operations.py中的last_insert_id函数,参考如下写法。

def last_insert_id(self, cursor, table_name, pk_name):

"""

Given a cursor object that has just performed an INSERT statement into

a table that has an auto-incrementing ID, returns the newly created ID.

This method also receives the table name and the name of the primary-key

column.

"""

sq_name = self._get_sequence_name(table_name)

cursor.execute('SELECT "%s".currval FROM dual' % sq_name)

if cursor.lastrowid is not None:

lastrowid=cursor.lastrowid

rowid_dict = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9,

'K': 10, 'L': 11,

'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20,

'V': 21, 'W': 22,

'X': 23, 'Y': 24, 'Z': 25, 'a': 26, 'b': 27, 'c': 28, 'd': 29, 'e': 30, 'f': 31,

'g': 32, 'h': 33,

'i': 34, 'j': 35, 'k': 36, 'l': 37, 'm': 38, 'n': 39, 'o': 40, 'p': 41, 'q': 42,

'r': 43, 's': 44,

't': 45, 'u': 46, 'v': 47, 'w': 48, 'x': 49, 'y': 50, 'z': 51, '0': 52, '1': 53,

'2': 54, '3': 55,

'4': 56, '5': 57, '6': 58, '7': 59, '8': 60, '9': 61, '+': 62, '/': 63}

rowid_temp = 0

for i in lastrowid[-8:]:

rowid_temp = rowid_temp * 64 + rowid_dict[i]

lastrowid=rowid_temp

query = 'select %s from %s where rowid = %s' %(self.quote_name(pk_name), self.quote_name(table_name), lastrowid)

cursor.execute(query)

else:

cursor.execute('SELECT MAX(%s) from %s' %(self.quote_name(pk_name), self.quote_name(table_name)))

value = cursor.fetchone()[0]

return value

错误七、TypeError: DatabaseSchemaEditor._alter_column_type_sql() takes 5 positional arguments but 7 were given

上图是新老版本的_alter_column_type_sql的函数定义,发现多了2个参数,分别是old_collation, new_collation。这2个参数在函数体内调用如下:

可以很容易定位到_collate_sql函数,后面2个参数压根未使用,只是使用了collation,也就是new_collation参数,这个函数的作用是字符集排序;

查看django_dmPython/schema.py中的_alter_column_type_sql函数,在参数中添加2个参数,这2个参数暂时先不用,后面有问题再处理(可以参考django/db/backends/postgresql/schema.py或django/db/backends/mysql/schema.py中同名函数的实现逻辑)。

相关推荐
hakesashou17 分钟前
python如何比较字符串
linux·开发语言·python
_.Switch1 小时前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
Hoper.J1 小时前
PyTorch 模型保存与加载的三种常用方式
人工智能·pytorch·python
弱冠少年2 小时前
websockets库使用(基于Python)
开发语言·python·numpy
向上的车轮2 小时前
Django学习笔记五:templates使用详解
笔记·学习·django
技术无疆2 小时前
【Python】Streamlit:为数据科学与机器学习打造的简易应用框架
开发语言·人工智能·python·深度学习·神经网络·机器学习·数据挖掘
羊小猪~~2 小时前
机器学习/数据分析--用通俗语言讲解时间序列自回归(AR)模型,并用其预测天气,拟合度98%+
人工智能·python·机器学习·数据挖掘·数据分析·回归·时序数据库
qq_273900233 小时前
解析TMalign文本文件中的转换矩阵
python·生物信息学
阿华的代码王国3 小时前
【JavaEE】——文件IO的应用
开发语言·python
电饭叔4 小时前
《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学
开发语言·python