Internals
Apart from special parameters defined in the Model
during definition (tablename, metadata etc.) the Model
provides you with useful internals.
Pydantic Model
All Model
classes inherit from pydantic.BaseModel
so you can access all normal attributes of pydantic models.
For example to list pydantic model fields you can:
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 | import databases
import sqlalchemy
import ormar
database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
class Course(ormar.Model):
class Meta:
database = database
metadata = metadata
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
print(Course.__fields__)
"""
Will produce:
{'id': ModelField(name='id',
type=Optional[int],
required=False,
default=None),
'name': ModelField(name='name',
type=Optional[str],
required=False,
default=None),
'completed': ModelField(name='completed',
type=bool,
required=False,
default=False)}
"""
|
Tip
Note how the primary key id
field is optional as Integer
primary key by default has autoincrement
set to True
.
Info
For more options visit official pydantic documentation.
Sqlalchemy Table
To access auto created sqlalchemy table you can use Model.Meta.table
parameter
For example to list table columns you can:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 | import databases
import sqlalchemy
import ormar
database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
class Course(ormar.Model):
class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy
database = database
metadata = metadata
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
print(Course.Meta.table.columns)
"""
Will produce:
['courses.id', 'courses.name', 'courses.completed']
"""
|
Tip
You can access table primary key name by Course.Meta.pkname
Fields Definition
To access ormar Fields
you can use Model.Meta.model_fields
parameter
For example to list table model fields you can:
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 | import databases
import sqlalchemy
import ormar
database = databases.Database("sqlite:///db.sqlite")
metadata = sqlalchemy.MetaData()
class Course(ormar.Model):
class Meta(ormar.ModelMeta):
database = database
metadata = metadata
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
print({x: v.__dict__ for x, v in Course.Meta.model_fields.items()})
"""
Will produce:
{'completed': mappingproxy({'autoincrement': False,
'choices': set(),
'column_type': Boolean(),
'default': False,
'index': False,
'name': 'completed',
'nullable': True,
'primary_key': False,
'pydantic_only': False,
'server_default': None,
'unique': False}),
'id': mappingproxy({'autoincrement': True,
'choices': set(),
'column_type': Integer(),
'default': None,
'ge': None,
'index': False,
'le': None,
'maximum': None,
'minimum': None,
'multiple_of': None,
'name': 'id',
'nullable': False,
'primary_key': True,
'pydantic_only': False,
'server_default': None,
'unique': False}),
'name': mappingproxy({'allow_blank': False,
'autoincrement': False,
'choices': set(),
'column_type': String(max_length=100),
'curtail_length': None,
'default': None,
'index': False,
'max_length': 100,
'min_length': None,
'name': 'name',
'nullable': False,
'primary_key': False,
'pydantic_only': False,
'regex': None,
'server_default': None,
'strip_whitespace': False,
'unique': False})}
"""
|
Info
Note that fields stored on a model are classes
not instances
.
So if you print just model fields you will get:
{'id': <class 'ormar.fields.model_fields.Integer'>,
'name': <class 'ormar.fields.model_fields.String'>,
'completed': <class 'ormar.fields.model_fields.Boolean'>}