我正在尝试连接一个PSQL表,其中peewee有一个名为“columnQL”的"money“价格。每次我连接并保存一个对象时,价格列都保持空白,但其他字段保存得很好。我在这里做错了什么?
我使用的是PostgresSQL 10.5和Peewee 3.9.3
from peewee import *
# Just returning the actual value for now. Do I just need to convert?
class CurrencyField(DoubleField):
def db_value(self, value):
return super().db_value(value)
def python_value(self, value):
return super().python_value(value)
db = PostgresqlDatabase('testdb', user='guest', field_types={CurrencyField: 'MONEY'})
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField()
class Product(BaseModel):
price = CurrencyField
name = CharField(max_length=355)```发布于 2019-08-25 05:14:59
首先,你的方法覆盖什么也不做,所以去掉它们。然后添加postgres中使用的字段类型:
class CurrencyField(DoubleField):
field_type = 'MONEY'然后在您的模型中,您不会实例化该字段(字段后面没有括号)。你需要使用一个字段instance...not这个类。
class Product(BaseModel):
price = CurrencyField() # ADD PARENS
name = CharField(max_length=355)你不需要注册任何东西。上面的代码就是您所需要的。
https://stackoverflow.com/questions/57565162
复制相似问题