我正在尝试在我的 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'
我是一名优秀的程序员,十分优秀!