-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodels.py
74 lines (58 loc) · 2.05 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
65
66
67
68
69
70
71
72
73
74
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
age = db.Column(db.Integer, nullable=False)
bio = db.Column(db.String(200), nullable=True)
password = db.Column(db.String(10), nullable=False)
email = db.Column(db.String(20), nullable=False)
def __repr__(self):
return "<User %r>" % self.password
def serialize(self):
return {
"id": self.id,
"name": self.name,
"age":self.age,
"bio":self.bio,
"email":self.email
}
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
body = db.Column(db.String(250), nullable=False)
publication_date = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
category_id = db.Column(db.Integer, db.ForeignKey('category.id'),
nullable=False)
category = db.relationship('Category',
backref=db.backref('posts', lazy=True))
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
user = db.relationship('User', backref=db.backref('users', lazy=True))
def __repr__(self):
return "<Post %r>" % self.title
def serialize(self):
return {
"id":self.id,
"title":self.title,
"body": self.body,
"publication_date":self.publication_date,
"category_id":self.category_id,
"user_id": self.user_id
}
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
def __repr__(self):
return "<Category %r>" % self.name
def serialize(self):
return{
"id":self.id,
"name":self.name,
}
def serializer(self):
return{
"id":self.id,
"name":self.name,
}