简体   繁体   中英

Simple Peewee ORM Python Bug - save hook

I'm using the Playhouse extension for Peewee , specifically signals, so that I can use @pre_save and @post_save hooks.

I configured it just as the docs say, but for some reason, exceptions are still getting thrown.


Excerpt from the code:

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)

I'm creating an instance as such:

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).

I don't understand how I could be getting an IntegrityError if I'm populating the slug field in the hook. The other issue is, not even the print() statement from the pre_save hook is running.

Is there anyway I could've set this up wrong?

Did you make sure your BaseModel inherits the signals.Model class?

http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#signals

To use the signals, you will need all of your project's models to be a subclass of playhouse.signals.Model, which overrides the necessary methods to provide support for the various signals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM