Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
comb:webapp0 [2014/07/13 14:02] alfred [Relationships with SQLAlchemy] |
comb:webapp0 [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 46: | Línea 46: | ||
| ===== Model ===== | ===== Model ===== | ||
| + | ==== Interact with model classes ==== | ||
| + | Only importing the code file and using the class you will be interacting with the data base: | ||
| + | <code python> | ||
| + | from person_app import Person | ||
| + | from person_app import db | ||
| + | p = Person(name='Juan', age=33) | ||
| + | Person.query.all() | ||
| + | db.session.add(p) | ||
| + | db.session.commit() | ||
| + | </code> | ||
| ==== Relationships with SQLAlchemy ==== | ==== Relationships with SQLAlchemy ==== | ||
| === One to one === | === One to one === | ||
| Línea 82: | Línea 92: | ||
| class Tag(db.Model): | class Tag(db.Model): | ||
| id = db.Column(db.Integer, primary_key=True) | id = db.Column(db.Integer, primary_key=True) | ||
| + | </code> | ||
| + | === Define fields === | ||
| + | <code python> | ||
| + | class Deck(db.Model): | ||
| + | id = db.Column(db.Integer, primary_key=True) | ||
| + | name = db.Column(db.Text, nullable=False) | ||
| + | date = db.Column(db.DateTime, default=datetime.now()) | ||
| + | cards = db.relationship('Card', backref='deck', lazy='dynamic') | ||
| + | |||
| + | |||
| + | class Card(db.Model): | ||
| + | id = db.Column(db.Integer, primary_key=True) | ||
| + | id_deck = db.Column(db.Integer, | ||
| + | db.ForeignKey('deck.id'), | ||
| + | nullable=False) | ||
| + | front = db.Column(db.Text) | ||
| + | rever = db.Column(db.Text) | ||
| + | </code> | ||
| + | === Separate model in another file === | ||
| + | The ''flaskext.sqlalchemy'' module does not have to be initialized with the app right away - you can do this instead: | ||
| + | <code python> | ||
| + | # apps.members.models | ||
| + | from flaskext.sqlalchemy import SQLAlchemy | ||
| + | |||
| + | db = SQLAlchemy() | ||
| + | |||
| + | class Member(db.Model): | ||
| + | # fields here | ||
| + | pass | ||
| + | </code> | ||
| + | And then in your application setup you can call init_app: | ||
| + | <code python> | ||
| + | # apps.application.py | ||
| + | from flask import Flask | ||
| + | from apps.members.models import db | ||
| + | |||
| + | app = Flask(__name__) | ||
| + | # later on | ||
| + | db.init_app(app) | ||
| + | </code> | ||
| + | |||
| + | Example: | ||
| + | <code python> | ||
| + | # apps.shared.models | ||
| + | from flaskext.sqlalchemy import SQLAlchemy | ||
| + | |||
| + | db = SQLAlchemy() | ||
| + | </code> | ||
| + | <code python> | ||
| + | # apps.members.models | ||
| + | from apps.shared.models import db | ||
| + | |||
| + | class Member(db.Model): | ||
| + | # TODO: Implement this. | ||
| + | pass | ||
| + | </code> | ||
| + | <code python> | ||
| + | # apps.reporting.members | ||
| + | from flask import render_template | ||
| + | from apps.members.models import Member | ||
| + | |||
| + | def report_on_members(): | ||
| + | # TODO: Actually use arguments | ||
| + | members = Member.filter(1==1).all() | ||
| + | return render_template("report.html", members=members) | ||
| + | </code> | ||
| + | <code python> | ||
| + | # apps.reporting.routes | ||
| + | from flask import Blueprint | ||
| + | from apps.reporting.members import report_on_members | ||
| + | |||
| + | reporting = Blueprint("reporting", __name__) | ||
| + | |||
| + | reporting.route("/member-report", methods=["GET","POST"])(report_on_members) | ||
| + | </code> | ||
| + | <code python> | ||
| + | # apps.application | ||
| + | from flask import Flask | ||
| + | from apps.shared import db | ||
| + | from apps.reporting.routes import reporting | ||
| + | |||
| + | app = Flask(__name__) | ||
| + | db.init_app(app) | ||
| + | app.register_blueprint(reporting) | ||
| + | </code> | ||
| + | |||
| + | Another example: | ||
| + | <code python> | ||
| + | app = Flask(__name__, static_path='') | ||
| + | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///digin.db' | ||
| + | db.init_app(app) | ||
| + | with app.app_context(): | ||
| + | db.create_all() | ||
| + | </code> | ||
| + | ===== HTML and JavaScript ===== | ||
| + | ==== Install JS libraries ==== | ||
| + | <code> | ||
| + | $ sudo apt-get install nodejs | ||
| + | $ sudo apt-get install npm | ||
| + | $ sudo npm install -g bower | ||
| + | $ sudo ln -s /usr/bin/nodejs /usr/bin/node | ||
| + | Inside the static folder... | ||
| + | $ bower install angular | ||
| </code> | </code> | ||