[파이썬] Peewee Model hooks 및 overrides

Peewee is a lightweight ORM (Object Relational Mapping) library for Python that simplifies database interaction. It provides various features, including model hooks and overrides, which allow you to customize the behavior of your models.

In this blog post, we will explore how to use Peewee model hooks and overrides to extend and modify the default functionality of your models.

What are Model Hooks?

Model hooks are callback functions that are automatically invoked at specific points in the lifecycle of a model. They allow you to execute custom logic before or after certain operations on the model, such as saving, deleting, or updating records.

Peewee provides several model hooks that you can override to add your own logic. Some commonly used model hooks include:

How to Use Model Hooks?

To use model hooks in Peewee, you need to define the hook function in your model class and override the respective hook method. Here’s an example:

from peewee import *

class User(Model):
    username = CharField()
    password = CharField()

    def before_save(self):
        # Custom logic before saving
        self.password = encrypt_password(self.password)

    def after_save(self):
        # Custom logic after saving
        send_email(self.username, 'Your account has been saved.')

    # ... other model fields and methods

# Create a new user and save it
user = User(username='john', password='password123')
user.save()

In the above example, we have a User model with before_save and after_save hooks overridden. The before_save hook is executed before saving the model, where we encrypt the user’s password. The after_save hook is executed after saving the model, where we send an email notification to the user.

By overriding different model hooks, you can easily customize and enhance the behavior of your models based on your specific requirements.

What are Model Overrides?

Model overrides allow you to completely replace or modify the default behavior of certain methods on your model class. This is useful when you need to extend or customize the functionality of Peewee model methods.

Peewee provides various model methods that you can override, such as save, delete_instance, update, etc.

How to Use Model Overrides?

To use model overrides in Peewee, you need to define the overridden method in your model class. Here’s an example:

from peewee import *

class User(Model):
    username = CharField()
    password = CharField()

    def save(self, *args, **kwargs):
        # Custom logic before saving
        self.password = encrypt_password(self.password + '_modified')
        super(User, self).save(*args, **kwargs)

    # ... other model fields and methods

# Create a new user and save it
user = User(username='john', password='password123')
user.save()

In the above example, we have overridden the save method to modify the user’s password before saving. We add “_modified” to the password before encrypting it and saving the model using the super method.

By overriding different model methods, you can extend or modify the default behavior provided by Peewee to meet your specific needs.

Conclusion

Peewee’s model hooks and overrides provide a powerful way to customize and extend the behavior of your models in Python. With the ability to execute custom logic before or after certain operations, you have fine-grained control over database interaction.

By leveraging model hooks and overrides in Peewee, you can build more expressive and flexible applications that adhere to specific business requirements.