【问题标题】:What is model class of SQLAlchemy?什么是 SQLAlchemy 的模型类?
【发布时间】:2014-03-31 17:42:44
【问题描述】:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'

db = SQLAlchemy(app)                             


class User(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return '<User %r>' % self.username

我在 SQLAlchemy 文档中找到了这段代码。 db=SQLAlchemy(app) 表示 db 是一个对象。 class User(db.Model) 通过查看该行看起来 db 是一个模块名称。

db 基本上是什么?谁能解释一下。

https://developers.google.com/appengine/docs/python/datastore/modelclass 我读过这个,但无法理解。

【问题讨论】:

  • 请在 python.org 上做 Python 初学者教程。你缺乏基础知识。 :)

标签: python sqlalchemy flask flask-sqlalchemy


【解决方案1】:

db = SQLAlchemy(app) 是用正确的应用上下文设置db变量的方法,基本上是在db实例上注册应用程序,我们用它来关联绑定到当前上下文的应用程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2020-02-08
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    相关资源
    最近更新 更多