[英]How do I register a custom peewee field as a postgres money type
I'm trying to connect to a PSQL table with peewee that has a "money" columnQL named "price". 我正在尝试使用具有名为“ price”的“ money” columnQL的peewee连接到PSQL表。 Every time I connect and save an object, the price column remains blank but the other fields are saved just fine. 每次我连接并保存一个对象时,price列都将保留为空白,而其他字段则保存得很好。 What am I doing wrong here? 我在这里做错了什么?
I'm using PostgresSQL 10.5 and Peewee 3.9.3 我正在使用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)```
First of all your method overrides do nothing so get rid of them. 首先,您的方法重写不执行任何操作,因此请摆脱它们。 Then add the field type as it is used in postgres: 然后添加在postgres中使用的字段类型:
class CurrencyField(DoubleField):
field_type = 'MONEY'
Then in your model, you are not instantiating the field (no parentheses after the field). 然后,在模型中,您无需实例化该字段(该字段后没有括号)。 You need to use a field instance...not the class. 您需要使用字段实例,而不是类。
class Product(BaseModel):
price = CurrencyField() # ADD PARENS
name = CharField(max_length=355)
You don't need to register anything. 您不需要注册任何东西。 The above code is all you need. 上面的代码就是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.