English 中文(简体)
Peewee - PostgreSQL & MySQL Extensions
  • 时间:2024-11-05

Peewee - PostgreSQL and MySQL Extensions


Previous Page Next Page  

Additional PostgreSQL functionapty is enabled by helpers which are defined in playhouse.postgres_ext module. This module defines PostgresqlExtDatabase class and provides the following additional field types to be exclusively used for declaration of model to be mapped against PostgreSQL database table.

Features of PostgreSQL Extensions

The features of PostgreSQL Extensions which are supported by Peewee are as follows −

    ArrayField field type, for storing arrays.

    HStoreField field type, for storing key/value pairs.

    IntervalField field type, for storing timedelta objects.

    JSONField field type, for storing JSON data.

    BinaryJSONField field type for the jsonb JSON data type.

    TSVectorField field type, for storing full-text search data.

    DateTimeTZField field type, a timezone-aware datetime field.

Additional Postgres-specific features in this module are meant to provide.

    hstore support.

    server-side cursors.

    full-text search.

Postgres hstore is a key:value store that can be embedded in a table as one of the fields of type HStoreField. To enable hstore support, create database instance with register_hstore=True parameter.


db = PostgresqlExtDatabase( mydatabase , register_hstore=True)

Define a model with one HStoreField.


class Vehicles(BaseExtModel):
   type = CharField()
   features = HStoreField()

Create a model instance as follows −


v=Vechicle.create(type= Car , specs:{ mfg : Maruti ,  Fuel : Petrol ,  model : Alto })

To access hstore values −


obj=Vehicle.get(Vehicle.id=v.id)
print (obj.features)

MySQL Extensions

Alternate implementation of MysqlDatabase class is provided by MySQLConnectorDatabase defined in playhouse.mysql_ext module. It uses Python’s DB-API compatible official mysql/python connector.


from playhouse.mysql_ext import MySQLConnectorDatabase

db = MySQLConnectorDatabase( mydatabase , host= localhost , user= root , password=  )
Advertisements