Last active
December 16, 2015 15:49
-
-
Save diffused/5458784 to your computer and use it in GitHub Desktop.
Postgresql array column types in python peewee orm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from peewee import * | |
db = PostgresqlDatabase(None) | |
# custom type for postgres Text[] array column | |
class TextArrayField(Field): | |
db_field = 'Text[]' | |
def db_value(self, value): | |
return value | |
def python_value(self, value): | |
return value | |
class BaseModel(Model): | |
class Meta: | |
database = db | |
class ProductType(BaseModel): | |
name = CharField() | |
sysname = CharField() | |
class Meta: | |
db_table = "product_types" | |
class Product(BaseModel): | |
name = CharField() | |
sysname = CharField() | |
sku = CharField() | |
# Text[] array column | |
image_colors_hex = TextArrayField() | |
product_type = ForeignKeyField(ProductType, related_name="product_type") | |
class Meta: | |
db_table = "products" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment