-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
70 lines (44 loc) · 1.74 KB
/
model.py
File metadata and controls
70 lines (44 loc) · 1.74 KB
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
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Melon(db.Model):
"""Melon"""
__tablename__ = 'melons'
melon_code = db.Column(db.String(8), primary_key=True)
name = db.Column(db.String, nullable=False)
price = db.Column(db.Float, nullable=False)
image_url = db.Column(db.String, nullable=False)
color = db.Column(db.String, nullable=False)
seedless = db.Column(db.Boolean, nullable=False)
melon_type_id = db.Column(db.Integer, db.ForeignKey('types.type_id'))
melon_type = db.relationship('MelonType', back_populates='melons')
def __repr__(self):
return f'<Melon melon_code={self.melon_code}>'
def to_dict(self):
return {'melon_code': self.melon_code,
'name': self.name,
'price': self.price,
'image_url': self.image_url,
'color': self.color,
'seedless': self.seedless,
'melon_type': self.melon_type.name}
class MelonType(db.Model):
"""Type of melon."""
__tablename__ = 'types'
type_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
name = db.Column(db.String, nullable=False)
melons = db.relationship('Melon', back_populates='melon_type')
def __repr__(self):
return f'<MelonType name={self.name}>'
def to_dict(self):
return {'type_id': self.type_id,
'name': self.name}
def connect_to_db(app):
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///melons'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
db.app = app
db.init_app(app)
print('Connected to database!')
if __name__ == '__main__':
from server import app
connect_to_db(app)