gpt4 book ai didi

python - pee wee Meta 子类继承

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:16 25 4
gpt4 key购买 nike

我正在尝试在我的 python 应用程序中实现 peewee,并且在这样定义我的类时:

import datetime
import peewee as pw
import acme.core as acme

adapter = pw.MySQLDatabase(
acme.get_config(path='database.db'),
host=acme.get_config(path='database.host'),
port=int(acme.get_config(path='database.port', default=3306)),
user=acme.get_config(path='database.user'),
passwd=acme.get_config(path='database.password'))


class Model(pw.Model):
"""
The base model that will connect the database
"""
id = pw.PrimaryKeyField()
created_at = pw.DateTimeField()
updated_at = pw.DateTimeField(default=datetime.datetime.now)

class Meta:
database = adapter


class ServerModule(Model):
enabled = pw.BooleanField()
ipaddr = pw.IntegerField()
port = pw.IntegerField()

class Meta(Model.Meta):
db_table = 'module_server'

我收到以下错误:

Traceback (most recent call last):
File "db.py", line 25, in <module>
class ServerModule(Model):
File "db.py", line 33, in ServerModule
class Meta(Model.Meta):
AttributeError: type object 'Toto' has no attribute 'Meta'

我已经尝试了基本的 python 子类继承并且它有效,但在这里它不起作用,有人可以指出我正确的方向吗?

最佳答案

您不需要从父 Meta 属性继承 Meta 类。 Meta.database 等属性自动继承。在你的例子中:

import datetime
import peewee as pw
import acme.core as acme

adapter = pw.MySQLDatabase(
acme.get_config(path='database.db'),
host=acme.get_config(path='database.host'),
port=int(acme.get_config(path='database.port', default=3306)),
user=acme.get_config(path='database.user'),
passwd=acme.get_config(path='database.password'))


class Model(pw.Model):
"""
The base model that will connect the database
"""
id = pw.PrimaryKeyField()
created_at = pw.DateTimeField()
updated_at = pw.DateTimeField(default=datetime.datetime.now)

class Meta:
database = adapter


class ServerModule(Model):
enabled = pw.BooleanField()
ipaddr = pw.IntegerField()
port = pw.IntegerField()

class Meta:
db_table = 'module_server'

关于python - pee wee Meta 子类继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33561682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com