Skip to main content

A module to expose the structure of SQLAlchemy models as a RESTful API

Project description

A simple Python module to expose SQLAlchemy model structures as a RESTful API.

## Example
Python model:

class Book(db.Model):
__tablename__ = 'books'

id = db.Column(db.Integer, primary_key=True)
time_created = db.Column(db.DateTime)
name = db.Column(db.String(140), unique=True)
description = db.Column(db.String(320))
in_inventory = db.Column(db.Boolean, default=True)
user_id = db.Column(db.Integer)
author = db.Column(db.String(140))

def __init__(self, name, author, description, in_inventory, user_id): = name
self.description = description
self.in_inventory = in_inventory
self.user_id = user_id
self.time_created = = author

def __repr__(self):
return '<Book %r>' %

JSON exposed via a RESTful API for this model:

"structure": {
"author": "VARCHAR(140)",
"description": "VARCHAR(320)",
"id": "INTEGER",
"in_inventory": "BOOLEAN",
"name": "VARCHAR(140)",
"time_created": "DATETIME",
"user_id": "INTEGER"

## Install

pip install modelstruct

## Usage

1. Import the Python module
from modelstruct import get_json

2. Create a new route for the endpoint you want to be your API (below the example is in Flask, and creates the API at the location `/models`, with the model class name `MODEL_NAME_HERE`)

@app.route('/models', methods=['GET'])
def get_model_structure():
return get_json(MODEL_NAME_HERE)

3. That's it! Send a GET request to the endpoint and get the model's structure.

Project details

Download files

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

Source Distribution

modelstruct-1.0.0.tar.gz (3.1 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page