forked from bustawin/sqlalchemy-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_inheritance.py
More file actions
93 lines (67 loc) · 2.84 KB
/
c_inheritance.py
File metadata and controls
93 lines (67 loc) · 2.84 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, make_response
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext.declarative import declared_attr
app = Flask(__name__) # Create Flask App
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://test:test@localhost/test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
class MySQLAlchemy(SQLAlchemy):
UUID = postgresql.UUID # Add psql's UUID type
CASCADE_DEL = 'save-update, merge, delete-orphan'
db = MySQLAlchemy(app, session_options={'autoflush': False})
class Computer(db.Model):
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.Unicode(), nullable=False)
model = db.Column(db.Unicode)
manufacturer = db.Column(db.Unicode)
serial_number = db.Column(db.Unicode)
def __str__(self) -> str:
return f'Computer {self.id} S/N {self.serial_number}'
def __repr__(self) -> str:
return str(self)
@declared_attr
def __mapper_args__(cls):
"""Defines inheritance.
From `the guide <http://docs.sqlalchemy.org/en/latest/orm/
extensions/declarative/api.html
#sqlalchemy.ext.declarative.declared_attr>`_
"""
# This method is only set up in the base class
# This + Desktop.id creates a "Join Table inheritance"
args = {'polymorphic_identity': cls.__name__}
if cls.__name__ == 'Computer':
# The attribute type is a string that automatically stores
# the name of the subclass the object is (i.e. "Desktop" / "Laptop")
args['polymorphic_on'] = cls.type
return args
class Desktop(Computer):
id = db.Column(db.Integer, db.ForeignKey(Computer.id), primary_key=True)
internal_lightning = db.Column(db.Boolean)
def __str__(self) -> str:
return f'Desktop {self.id} S/N {self.serial_number}'
class Laptop(Computer):
id = db.Column(db.Integer, db.ForeignKey(Computer.id), primary_key=True)
keyboard_layout = db.Column(db.Unicode)
def __str__(self) -> str:
return f'Laptop {self.id} S/N {self.serial_number}'
db.drop_all()
db.create_all()
@app.route('/', methods={'POST'})
def create_computers():
"""Creates two types of PCs, a desktop and a laptop, and returns them."""
desktop = Desktop(serial_number='d1', internal_lightning=True)
laptop = Laptop(serial_number='l1', keyboard_layout='USA')
db.session.add(desktop)
db.session.add(laptop)
db.session.flush()
r = make_response(f'{desktop}\n{laptop}')
db.session.commit()
return r
@app.route('/desktops/', methods={'GET'})
def get_desktops():
return make_response(str(tuple(Desktop.query)))
client = app.test_client()
print('Create 2 subclasses of Computers: 1 Desktop and 1 Laptop')
print('Response:', client.post('/').data)
print('Get all desktops:')
print('Response:', client.get('/desktops/').data)