[파이썬] Peewee Custom Field 정의

In this blog post, we will explore how to define custom fields in Peewee, a lightweight Python Object-Relational Mapping (ORM) library.

Peewee provides a wide range of built-in field types such as CharField, IntegerField, ForeignKeyField, etc. However, there may be scenarios when these built-in fields do not fully meet our requirements. In such cases, we can define custom fields to handle specific data types or implement complex functionality.

To define a custom field in Peewee, we need to create a new class that inherits from peewee.Field. This custom field class should implement methods for serialization, deserialization, and validation.

Here’s an example of how you can define a custom field for storing and working with JSON data using Peewee:

import json
from peewee import Field


class JSONField(Field):
    """Custom field for storing JSON data."""

    def db_value(self, value):
        """Convert Python object to database value."""
        return json.dumps(value)

    def python_value(self, value):
        """Convert database value to Python object."""
        return json.loads(value)

In the above example, we define a JSONField class that inherits from Field. We override the db_value method to convert the Python object to a JSON string before storing it in the database. Similarly, we override the python_value method to convert the JSON string back to a Python object when retrieving data from the database.

Once we have defined the custom field, we can use it in our Peewee models like any other built-in field. Here’s an example of using the JSONField in a Peewee model:

from peewee import Model, MySQLDatabase
from playhouse.shortcuts import model_to_dict

db = MySQLDatabase('my_database', user='my_user', password='my_password')


class MyModel(Model):
    name = CharField()
    config = JSONField()

    class Meta:
        database = db

In the above example, we define a MyModel class with a name field of type CharField and a config field of type JSONField. The config field will be used to store JSON data in the database.

We can then work with instances of MyModel and access the config field as a Python object. Here’s an example:

# Create a new instance of MyModel
my_instance = MyModel(name='example', config={'key': 'value'})

# Save the instance to the database
my_instance.save()

# Retrieve the instance from the database
retrieved_instance = MyModel.get(MyModel.name == 'example')

# Access the config field as a Python object
config_data = retrieved_instance.config

# Modify the config field and save the changes
config_data['new_key'] = 'new_value'
retrieved_instance.save()

In the above example, we create a new instance of MyModel with a name and a config field containing a JSON object. We then save the instance to the database, retrieve it later, and access the config field as a Python object. We can modify the config field and save the changes back to the database.

Defining custom fields in Peewee provides flexibility and allows us to work with non-standard data types or implement specialized behavior. By subclassing peewee.Field and implementing the necessary methods, we can define custom fields to seamlessly integrate with our Peewee models.

Peewee documentation: http://docs.peewee-orm.com/