Giter Site home page Giter Site logo

flask-restless-datamodel's Introduction

Build Status Codecov PyPI version

Purpose

This library is one part of a two part piece of code. It fulfills the server part to the flask-restless client. What it does is allow you to render your datamodel in a convenient JSON format. This JSON format is then read by the flask-restless-client, which in turn uses it to built itself, allowing for transparent access to your data model through HTTP.

Quickstart

Enabling this feature is as easy as registering an SQLAlchemy model in flask-restless. The only thing you need to do, is import the DataModel class from the library and use it to register your api.

import flask
import flask_restless
from flask_sqlalchemy import SQLAlchemy
from flask_restless_datamodel import DataModel
from my_models import Person, Computer, db

app = flask.Flask(__name__)
db = SQLAlchemy(app)

# Create a datamodel instance to register later
data_model = DataModel(manager)

manager = flask_restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Person, methods=['GET'], include_columns=['name'])
manager.create_api(Computer, methods=['GET'], collection_name='compjutahs', exclude_columns=['name'])
manager.create_api(data_model, methods=['GET'])
# In case you want to expose the methods and properties as well
data_model.register_rpc_blueprint()

Which will expose an endpoint http://localhost:5000/flask-restless-datamodel which in turn will yield a result as followed

{
   "Computer":{
      "attributes":{
         "id":"integer",
         "owner_id":"integer",
         "owner_name":"unicode",
         "purchase_time":"datetime",
         "vendor":"unicode"
      },
      "collection_name":"compjutahs",
      "methods":{},
      "pk_name":"id",
      "relations":{
         "owner":{
            "backref":"computers",
            "foreign_model":"Person",
            "local_column":"owner_id",
            "relation_type":"MANYTOONE"
         },
         "peers":{
            "foreign_model":"Computer",
            "is_proxy":true,
            "relation_type":"MANYTOONE"
         }
      }
   },
   "Person":{
      "attributes":{
         "name":"unicode"
      },
      "collection_name":"person",
      "methods":{},
      "pk_name":"id",
      "relations":{

      }
   }
}

This result will be used by the client code to build models on the fly.

flask-restless-datamodel's People

Contributors

bertrandchenal avatar jessedeloore avatar maarten-dp avatar maartendp avatar pierresavatte avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.