forked from jennielees/flask-sqlalchemy-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
56 lines (37 loc) · 1.38 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
from app import db
class Dessert(db.Model):
# See http://flask-sqlalchemy.pocoo.org/2.0/models/#simple-example
# for details on the column types.
# We always need an id
id = db.Column(db.Integer, primary_key=True)
# A dessert has a name, a price and some calories:
name = db.Column(db.String(100))
price = db.Column(db.Float)
calories = db.Column(db.Integer)
def __init__(self, name, price, calories):
self.name = name
self.price = price
self.calories = calories
def calories_per_dollar(self):
if self.calories:
return self.calories / self.price
class Menu(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
def __init__(self, name):
self.name = name
def create_dessert(new_name, new_price, new_calories):
# Create a dessert with the provided input.
# At first, we will trust the user.
# This line maps to line 16 above (the Dessert.__init__ method)
dessert = Dessert(new_name, new_price, new_calories)
# Actually add this dessert to the database
db.session.add(dessert)
# Save all pending changes to the database
db.session.commit()
return dessert
if __name__ == "__main__":
# Run this file directly to create the database tables.
print "Creating database tables..."
db.create_all()
print "Done!"