[파이썬] Peewee 조건 및 필터링

Peewee is a lightweight and expressive ORM (Object Relational Mapping) library for Python. It provides a simple and intuitive way to interact with databases in your Python applications. In this blog post, we will focus on the “조건 및 필터링” feature of Peewee.

Filtering Rows

One of the most common tasks when working with databases is to retrieve specific rows based on certain criteria. Peewee makes this task easy by providing a fluent API for filtering rows.

Let’s say we have a Person model with three fields: name, age, and city. We can use the select() method to retrieve all rows from the Person table. In order to filter the results, we can chain the where() method and provide the filtering conditions.

from peewee import *

db = SqliteDatabase('mydatabase.db')

class Person(Model):
    name = CharField()
    age = IntegerField()
    city = CharField()

    class Meta:
        database = db

# Connect to the database and retrieve all persons whose age is greater than 30
persons = Person.select().where(Person.age > 30)

for person in persons:
    print(person.name, person.age, person.city)

In the above example, we connect to the database and retrieve all Person objects whose age is greater than 30.

Peewee supports a wide range of comparison operators such as equals, less than, greater than, less than or equal to, greater than or equal to, and more. You can use these operators to craft complex filtering conditions to suit your needs.

Combining Filters

Peewee allows you to combine multiple filters using logical operators such as and and or. This enables you to create more powerful and flexible queries.

# Retrieve all persons whose age is greater than 30 and city is 'New York'
persons = Person.select().where((Person.age > 30) & (Person.city == 'New York'))

# Retrieve all persons whose age is greater than 30 or city is 'San Francisco'
persons = Person.select().where((Person.age > 30) | (Person.city == 'San Francisco'))

In the above examples, we combine multiple filters using the & and | operators to retrieve specific rows from the Person table.

Custom Conditions

Peewee also allows you to define custom conditions using functions or lambda expressions. This gives you more control over the filtering process.

# Retrieve all persons whose name starts with 'J'
persons = Person.select().where(lambda person: person.name.startswith('J'))

# Custom function to check if a person is above a certain age
def is_above_age(person, age):
    return person.age > age

# Retrieve all persons whose age is greater than 40 using the custom function
persons = Person.select().where(lambda person: is_above_age(person, 40))

In the above examples, we create custom conditions using lambda expressions and functions to filter the Person objects.

Conclusion

Peewee provides a powerful and flexible way to filter and query databases in your Python applications. The ability to combine filters, use custom conditions, and apply various comparison operators allows you to retrieve exactly the data you need. This makes Peewee a great choice for working with databases in Python projects.

For more information on Peewee and its features, you can refer to the official documentation here.