-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
64 lines (42 loc) · 1.83 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask
from flask_login import UserMixin
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog_new.db'
db = SQLAlchemy(app)
posts_tags_m2m_table = db.Table(
'posts_tags',
db.Column('post_id', db.Integer, db.ForeignKey('post.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)
)
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
title = db.Column(db.String(50), nullable=True)
posts = db.relationship('Post', backref='categories', lazy=True)
def __str__(self):
return str(self.title)
def __repr__(self):
return self.__str__()
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
picture = db.Column(db.String(250), nullable=True)
title = db.Column(db.String(75), nullable=False)
text = db.Column(db.Text, nullable=False)
is_published = db.Column(db.Boolean, nullable=False, default=True, server_default='1')
tags = db.relationship('Tag', secondary=posts_tags_m2m_table, lazy='subquery',
backref=db.backref('tags', lazy=True))
category_id = db.Column(db.Integer, db.ForeignKey(Category.id),
nullable=True)
def __str__(self):
return str(self.title)
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True, nullable=False)
title = db.Column(db.String(50), nullable=False, unique=True)
def __str__(self):
return str(self.title)
def __repr__(self):
return self.__str__()
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(128), nullable=False, unique=True)
password = db.Column(db.String(128), nullable=False)