我有一个创建连接池的数据库连接类。现在随着应用程序的增长并且我正在添加不同类型的数据库编写器,我想将数据库连接移动到一个单独的类并从它继承。到目前为止,我有这个:
class ServiceDB:
@classmethod
async def init(cls, settings):
self = ServiceDB()
self.pool = await asyncpg.create_pool(
database=settings["POSTGRES_DB"],
user=settings["POSTGRES_USER"],
password=settings["POSTGRES_PASSWORD"],
host=settings["DB_HOST"],
port=settings["DB_PORT"],
)
return self
class ChildWriter(ServiceDB):
async def write_db(self, query):
# Write to specific table
pass
if __name__ == "__main__":
settings = {'info': "some connection settings"}
query = "SELECT * FROM 'table'"
connection = await ChildWriter().init(settings)
await connection.write_db(msg, query)
当我运行它时,我得到AttributeError: 'ServiceDB' object has no attribute 'write_db'
. 如何正确扩展ServiceDB
该write_db
方法?