Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,16 @@ All fields are required unless one of the following is set:
The following column types are supported.
See TypeSystem for [type-specific validation keyword arguments][typesystem-fields].

* `orm.String(max_length)`
* `orm.Text()`
* `orm.BigInteger()`
* `orm.Boolean()`
* `orm.Integer()`
* `orm.Float()`
* `orm.Date()`
* `orm.Time()`
* `orm.DateTime()`
* `orm.Enum()`
* `orm.Float()`
* `orm.Integer()`
* `orm.String(max_length)`
* `orm.Text()`
* `orm.Time()`
* `orm.JSON()`

[sqlalchemy-core]: https://docs.sqlalchemy.org/en/latest/core/
Expand Down
2 changes: 2 additions & 0 deletions orm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Boolean,
Date,
DateTime,
Enum,
Float,
ForeignKey,
Integer,
Expand All @@ -22,6 +23,7 @@
"Boolean",
"Date",
"DateTime",
"Enum",
"Float",
"Integer",
"String",
Expand Down
9 changes: 9 additions & 0 deletions orm/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,12 @@ def expand_relationship(self, value):
if isinstance(value, self.to):
return value
return self.to({self.to.__pkname__: value})


class Enum(ModelField, typesystem.Any):
def __init__(self, enum, **kwargs):
super().__init__(**kwargs)
self.enum = enum

def get_column_type(self):
return sqlalchemy.Enum(self.enum)
13 changes: 12 additions & 1 deletion tests/test_columns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import datetime
import functools
from enum import Enum

import databases
import pytest
Expand All @@ -17,6 +18,11 @@ def time():
return datetime.datetime.now().time()


class StatusEnum(Enum):
DRAFT = "Draft"
RELEASED = "Released"


class Example(orm.Model):
__tablename__ = "example"
__metadata__ = metadata
Expand All @@ -30,6 +36,7 @@ class Example(orm.Model):
description = orm.Text(allow_blank=True)
value = orm.Float(allow_null=True)
data = orm.JSON(default={})
status = orm.Enum(StatusEnum, default=StatusEnum.DRAFT)


@pytest.fixture(autouse=True, scope="module")
Expand Down Expand Up @@ -66,8 +73,12 @@ async def test_model_crud():
assert example.description == ""
assert example.value is None
assert example.data == {}
assert example.status == StatusEnum.DRAFT

await example.update(data={"foo": 123}, value=123.456)
await example.update(
data={"foo": 123}, value=123.456, status=StatusEnum.RELEASED
)
example = await Example.objects.get()
assert example.value == 123.456
assert example.data == {"foo": 123}
assert example.status == StatusEnum.RELEASED