我正在尝试在我的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子类继承并且它可以工作,但是在这里它没有,有人能指出我正确的方向吗?
答案 0 :(得分:2)
您不需要从父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'