Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Reusable Python code for projects

Project description

KBPC

GitHub PyPI version Build Status Coverage Status

Table of contents

Database

Models

User Model

Docs coming soon

User DAO

Docs coming soon


Flask Alchemy Model Serializer

Transformation was originally a series of routines written to convert FlaskAlchemy models into jsonifiable dict structures. This proved to be a solution lacking elegance and evolved and eventually found its way into half a dozen projects over time and eventually, then started to splinter into slightly different variations.

Basic Usage

from flask import jsonify

from application.models import SomeFlaskAlchemyModel
from kbpc.db.flaskalchemy import serializer

# Assume the model has name and age as the model fields
model = SomeFlaskAlchemyModel('John Doe', 25)
transformed_model = serializer.serialize(model)

# This typically fails if you attempt it with the model.
json = jsonify(data=model)

# This however is serializable immediately
json = jsonify(data=transformed_model)

Often, fields like passwords or other sensitive data should be hidden from responses or outputs. Usually this would be managed at the model class, and writing a to_dict() function or something similar, and simply not declaring the protected properties of that instance.

Protected Properties

from flask import jsonify

from application.models import SomeFlaskAlchemyModel
from kbpc.db.flaskalchemy import serializer

# Assume the model has name and age as the model fields
model = SomeFlaskAlchemyModel('John Doe', 25)
tablename = 'tablename_of_model'
exclusions = {tablename: ['age']}
transformed_model = serializer.serialize(model, exclusions)

# This however is serializable immediately
json = jsonify(data=transformed_model)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for Flask-KBPCUtils, version 0.1.23
Filename, size File type Python version Upload date Hashes
Filename, size Flask-KBPCUtils-0.1.23.tar.gz (9.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page