[파이썬] Peewee Custom manager classes

Peewee is a lightweight and expressive Object-Relational Mapping (ORM) library for Python. It provides a simple and intuitive way to interact with databases. With Peewee, you can define models, query the database, and perform CRUD (Create, Read, Update, Delete) operations effortlessly.

In addition to the built-in query capabilities, Peewee also allows you to create custom manager classes. These manager classes can encapsulate complex queries or provide additional methods to simplify database interactions. In this blog post, we will explore how to create and use custom manager classes in Peewee.

What are manager classes?

Manager classes in Peewee are Python classes that define custom methods and queries for interacting with the database. These classes act as a middle layer between the models and the database, allowing you to extend the functionality provided by Peewee.

Creating a custom manager class

To create a custom manager class, you can subclass the peewee.Manager class and define your custom methods and queries. Let’s take a look at an example:

from peewee import Manager

class PublishedPostsManager(Manager):
    def get_published_posts(self):
        return self.get_query().where(Post.is_published == True)

class Post(Model):
    title = CharField()
    content = TextField()
    is_published = BooleanField(default=False)

    objects = PublishedPostsManager()

In the above code, we created a custom manager class called PublishedPostsManager. It defines a single method get_published_posts that retrieves all the published posts from the database. We then assign an instance of this manager class to the objects attribute of our Post model.

By doing this, we can now use the custom method get_published_posts directly on the Post model:

published_posts = Post.objects.get_published_posts()

Using custom manager methods

Once you have defined a custom manager class and assigned it to your model, you can use its methods just like the built-in methods provided by Peewee. Custom manager methods can be used to encapsulate complex queries or to provide additional functionality that is specific to your application.

Let’s extend our example to add another method that retrieves the most recent published post:

class PublishedPostsManager(Manager):
    def get_published_posts(self):
        return self.get_query().where(Post.is_published == True)

    def get_most_recent_published_post(self):
        return self.get_published_posts().order_by(Post.created_at.desc()).first()

In the above code, we added a new method get_most_recent_published_post that retrieves the most recent published post by ordering the published posts by their created_at timestamp in descending order and returning the first result.

We can now use this custom method on the Post model:

most_recent_post = Post.objects.get_most_recent_published_post()

Conclusion

Custom manager classes in Peewee provide a powerful way to extend the functionality of your models and simplify database interactions. By encapsulating complex queries or providing additional methods, you can make your code more readable, maintainable, and reusable.

In this blog post, we learned how to create custom manager classes in Peewee and use them to perform custom queries and retrieve specific data from the database. Feel free to experiment with custom managers and explore other possibilities that Peewee has to offer. Happy coding!