[英]Simple Peewee ORM Python Bug - save hook
我正在使用Peewee的Playhouse擴展程序 (特別是信號),以便可以使用@pre_save
和@post_save
掛鈎。
我按照文檔所述配置了它,但是由於某種原因,仍然會引發異常。
代碼摘錄:
from playhouse.signals import pre_save
from db.config import BaseModel
class Card(BaseModel):
name = CharField(max_length=18)
slug = CharField(max_length=18)
published = BooleanField(default=False)
category = ForeignKeyField(Category, backref='cards')
@pre_save(sender=Card)
def card_pre_save(model, instance, created):
print('testing hook')
if created:
instance.slug = slugify(instance.name)
我正在這樣創建一個實例:
from db import models as m
card = m.Card(
name=new_card_name,
category=category,
published=False
)
card.save()
And this is the error I'm getting:
peewee.IntegrityError: null value in column "slug" violates not-null constraint
DETAIL: Failing row contains (2, 2019-02-04 05:41:57.111115, 2019-02-04 05:41:37.75196, cool11, null, f, 2).
我不明白如果我在鈎子中填充slug
字段,怎么會收到IntegrityError
。 另一個問題是,甚至pre_save
掛鈎中的print()
語句都沒有運行。
無論如何,我可以將其設置為錯誤嗎?
您是否確定BaseModel
繼承了BaseModel
類?
http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#signals
要使用信號,您需要將所有項目模型都作為playhouse.signals.Model的子類,該類將覆蓋為各種信號提供支持的必要方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.