-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
55 lines (43 loc) · 1.57 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
from dbase import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(80), unique=False, nullable=False)
def __repr__(self):
return '<User {} {}>'.format(self.id, self.username)
@staticmethod
def add(username, password):
user = User(username=username, password=password)
db.session.add(user)
db.session.commit()
@staticmethod
def delete(obj):
db.session.delete(obj)
db.session.commit()
class News(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=False, nullable=False)
content = db.Column(db.String(80), unique=False, nullable=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
user = db.relationship('User', backref=db.backref('product_list', lazy=True))
def __repr__(self):
return '<Products {} {} {}>'.format(self.id, self.title, self.user_id)
@staticmethod
def add(title, content, user):
news = News(title=title, content=content, user=user)
db.session.add(news)
db.session.commit()
return news
@staticmethod
def delete(obj):
db.session.delete(obj)
db.session.commit()
@property
def serialize(self):
"""Return object data in easily serializable format"""
return {
'id': self.id,
'title': self.title,
'content': self.content,
'user_id': self.user_id
}