[파이썬] Peewee Advanced filtering techniques

Peewee is a lightweight and expressive ORM (Object-Relational Mapping) for Python. It provides a simple and intuitive way to interact with databases. In this blog post, we will explore some advanced filtering techniques using Peewee.

Basic Filtering with Peewee

Before diving into advanced filtering techniques, let’s quickly recap basic filtering with Peewee. Peewee provides an expressive syntax for filtering records based on various conditions.

Let’s consider an example where we have a User model with fields id, username, and age. To filter all users above the age of 18, we can use the following code:

from peewee import *

database = SqliteDatabase('mydatabase.db')

class User(Model):
    username = CharField()
    age = IntegerField()

    class Meta:
        database = database

# Connect to the database and create the tables
database.connect()
database.create_tables([User])

# Filtering users above the age of 18
users_above_18 = User.select().where(User.age > 18)

Advanced Filtering Techniques

Multiple Conditions

Peewee allows combining multiple conditions using logical operators such as AND and OR. Let’s consider an example where we want to filter users above the age of 18 with usernames starting with ‘J’:

# Filtering users above the age of 18 with usernames starting with 'J'
users_above_18_starting_with_J = User.select().where((User.age > 18) & (User.username.startswith('J')))

Case-insensitive Filtering

Peewee provides case-insensitive filtering. To perform a case-insensitive filter on the username field, we can use the fn function:

from peewee import fn

# Filtering users with usernames containing 'john' (case-insensitive)
users_with_john = User.select().where(fn.Lower(User.username).contains('john'))

Filtering with Subqueries

Peewee supports subqueries for advanced filtering. Let’s consider an example where we want to filter users based on the result of a subquery.

# Subquery to get usernames starting with 'J'
subquery = User.select().where(User.username.startswith('J'))

# Filtering users based on the subquery result
users_starting_with_J = User.select().where(User.username.in_(subquery))

Complex Filtering with Joins

Peewee allows complex filtering by leveraging joins with related models. Let’s consider an example where we have a Post model related to the User model, and we want to filter users who have at least one post:

class Post(Model):
    user = ForeignKeyField(User)
    title = CharField()

    class Meta:
        database = database

# Filtering users who have at least one post
users_with_posts = User.select().join(Post).group_by(User).having(fn.Count(Post.id) > 0)

Conclusion

Peewee provides powerful and flexible filtering capabilities to make querying databases in Python a breeze. In this blog post, we explored some advanced filtering techniques using Peewee, including multiple conditions, case-insensitive filtering, filtering with subqueries, and complex filtering with joins.

By leveraging these advanced filtering techniques, you can easily manipulate and analyze data in your database with the help of Peewee’s expressive syntax and ORM features.