Flask SQLAlchemy一对一,一对多的使用方法实践

WBOY
Release: 2016-06-16 08:46:48
Original
1180 people have browsed it

Flask-SQLAlchemy安装和建表操作请参考这里。

复制代码 代码如下:

# Role表
class Role(db.Model):
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(80))

# RoleType表
class Role_type(db.Model):
    query_class=Common_list_name_Query
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(120))

一对一
只需要在属性里改变下定义

复制代码 代码如下:

# Role表
class Role(db.Model):
    role_type_id=db.Column(db.Integer,db.ForeignKey('role_type.id'))

role=db.relationship('Role',backref='role_type',lazy='dynamic', uselist=False)

一对多

复制代码 代码如下:

# 一对多需要在两个表内斗填上相互的关系
class Role(db.Model):
    role_type_id=db.Column(db.Integer,db.ForeignKey('role_type.id'))

class Role_type(db.Model):
    roles=db.relationship('Role',backref='role_type',lazy='dynamic')

具体参数可以参考如下的文档:
http://flask.pocoo.org/docs/patterns/sqlalchemy/
http://packages.python.org/Flask-SQLAlchemy/

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!