SQL alchemy

form.py

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo


class RegistrationForm(FlaskForm):
    username = StringField('Username',
                           validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password',
                                     validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')


class LoginForm(FlaskForm):
    email = StringField('Email',
                        validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Me')
    submit = SubmitField('Login')

flaskblog.py

from datetime import datetime
from flask import Flask,render_template,url_for,flash,redirect
from forms import RegistrationForm, LoginForm
from flask_sqlalchemy import SQLAlchemy

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"

创建User

from flaskblog import User,Post
from flaskblog import db
user_1 = User(username='Corey',email='[email protected]',password='password')
db.session.add(user_1)
user_2 = User(username='JohnDoe',email='[email protected]',password='password')
db.session.add(user_2)
db.session.commit()

查询

User.query.all()

Out[12]:
[User('Corey', '[email protected]', 'default.jpg'),
 User('JohnDoe', '[email protected]', 'default.jpg')]
user=User.query.get(1)
user
Out[17]: User('Corey', '[email protected]', 'default.jpg')

user.id
Out[18]: 1

创建Post

post_1 = Post(title='Blog 1',content='First Post Content',user_id=user.id)
post_2 = Post(title='Blog 2',content='First Post Content',user_id=user.id)
db.session.add(post_1)
db.session.add(post_2)
db.session.commit()
user.posts
Out[25]:
[Post('Blog 1', '2019-01-25 06:52:30.079563'),
 Post('Blog 2', '2019-01-25 06:52:30.333877')]

In [26]: for post in user.posts:
    ...:     print(post.title)
Blog 1
Blog 2

post = Post.query.first()
In [28]: post
Out[28]: Post('Blog 1', '2019-01-25 06:52:30.079563')

In [29]: post.user_id
Out[29]: 1

In [30]: post.author
Out[30]: User('Corey', '[email protected]', 'default.jpg')

删除

In [31]: db.drop_all()

你可能感兴趣的:(SQL alchemy)