[파이썬] Peewee ManyToManyField 관계 설정

Peewee is a simple and lightweight Object-Relational Mapping (ORM) library for Python. It provides a convenient way to interact with databases using Python classes and objects. One of the powerful features of Peewee is its ability to handle Many-to-Many relationships between tables.

In this blog post, we will explore how to set up and use the ManyToManyField in Peewee to define and work with Many-to-Many relationships in our database models.

Setting up ManyToManyField

To begin with, let’s assume we have two tables in our database: User and Group. We want to establish a Many-to-Many relationship between these two tables, where a user can belong to multiple groups and a group can have multiple users.

We start by defining two Peewee models for each table:

from peewee import Model, CharField, ManyToManyField, SqliteDatabase

database = SqliteDatabase('mydatabase.db')

class User(Model):
    name = CharField()
    
    class Meta:
        database = database

class Group(Model):
    name = CharField()
    members = ManyToManyField(User)
    
    class Meta:
        database = database

In the above code, we imported the necessary classes from Peewee and created a SqliteDatabase object to connect to our database.

We then defined the User and Group models, which inherit from the Model class provided by Peewee. Each model represents a table in our database, and the fields within each model define the columns of the table.

The User model has a single field, name, which is of type CharField. The Group model also has a name field, but it also includes a members field of type ManyToManyField that establishes the Many-to-Many relationship between User and Group.

Working with ManyToManyField

Now that we have defined the models, let’s see how we can work with the ManyToManyField in our Python code.

Adding Members to a Group

To add a user to a group, we simply append the user object to the members field of the group object:

# Creating a user and a group
user = User(name="John Doe")
group = Group(name="Admins")

# Adding the user to the group
group.members.add(user)

Retrieving Members of a Group

To retrieve the members of a group, we can access the members field of the group object:

# Retrieving the members of a group
members = group.members

# Iterating over the members
for member in members:
    print(member.name)

Querying Groups of a User

To query for the groups a user belongs to, we can use the groups attribute of the user object:

# Retrieving the groups of a user
groups = user.groups

# Iterating over the groups
for group in groups:
    print(group.name)

Removing Members from a Group

To remove a user from a group, we can use the remove method of the members field:

# Removing a user from a group
group.members.remove(user)

Conclusion

Peewee’s ManyToManyField allows us to easily define and work with Many-to-Many relationships in our database models. It provides a straightforward way to add, retrieve, and remove members from a group.

In this blog post, we explored the setup and usage of the ManyToManyField in Peewee by creating a Many-to-Many relationship between a User and Group model. However, Peewee offers much more functionality and flexibility for working with databases in Python. I encourage you to dive deeper into Peewee’s documentation to explore all it has to offer.

Happy coding!