Skip to content

Latest commit

 

History

History
87 lines (68 loc) · 3.42 KB

File metadata and controls

87 lines (68 loc) · 3.42 KB

title: sqlalchemy.dialects.postgresql JSON Example Code category: page slug: sqlalchemy-dialects-postgresql-json-examples sortorder: 500031013 toc: False sidebartitle: sqlalchemy.dialects.postgresql JSON meta: Python example code that shows how to use the JSON constant from the sqlalchemy.dialects.postgresql module of the SQLAlchemy project.

JSON is a constant within the sqlalchemy.dialects.postgresql module of the SQLAlchemy project.

ARRAY, BIGINT, BIT, DOUBLE_PRECISION, ExcludeConstraint, INTEGER, TSVECTOR, array, json, and pypostgresql are several other callables with code examples from the same sqlalchemy.dialects.postgresql package.

Example 1 from sqlalchemy-utils

sqlalchemy-utils (project documentation and PyPI package information) is a code library with various helper functions and new data types that make it easier to use SQLAlchemy when building projects that involve more specific storage requirements such as currency. The wide array of data types includes ranged values and aggregated attributes.

sqlalchemy-utils / sqlalchemy_utils / types / json.py

# json.py
from __future__ import absolute_import

import six
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql.base import ischema_names

from ..exceptions import ImproperlyConfigured

json = None
try:
    import anyjson as json
except ImportError:
    import json as json

try:
~~    from sqlalchemy.dialects.postgresql import JSON
    has_postgres_json = True
except ImportError:
    class PostgresJSONType(sa.types.UserDefinedType):
        def get_col_spec(self):
            return 'json'

    ischema_names['json'] = PostgresJSONType
    has_postgres_json = False


class JSONType(sa.types.TypeDecorator):
    impl = sa.UnicodeText

    def __init__(self, *args, **kwargs):
        if json is None:
            raise ImproperlyConfigured(
                'JSONType needs anyjson package installed.'
            )
        super(JSONType, self).__init__(*args, **kwargs)

    def load_dialect_impl(self, dialect):
        if dialect.name == 'postgresql':
            if has_postgres_json:
                return dialect.type_descriptor(JSON())


## ... source file continues with no further JSON examples...