From d945a67f7a446df3563d68f4ae2b98ad99b0492a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BF=E8=BF=87=E5=AE=B6=E5=AE=B6?= <88415165+ProgrammerPlus1998@users.noreply.github.com> Date: Thu, 5 Aug 2021 16:38:34 +0800 Subject: [PATCH] Update model_fields.py add:SmallInteger() --- ormar/fields/model_fields.py | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index 07e9ed0..1196048 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -538,6 +538,54 @@ else: ) +class SmallInteger(Integer, int): + """ + SmallInteger field factory that construct Field classes and populated their values. + """ + + _type = int + _sample = 0 + + def __new__( # type: ignore + cls, + *, + minimum: int = None, + maximum: int = None, + multiple_of: int = None, + **kwargs: Any + ) -> BaseField: + autoincrement = kwargs.pop("autoincrement", None) + autoincrement = ( + autoincrement + if autoincrement is not None + else kwargs.get("primary_key", False) + ) + kwargs = { + **kwargs, + **{ + k: v + for k, v in locals().items() + if k not in ["cls", "__class__", "kwargs"] + }, + } + kwargs["ge"] = kwargs["minimum"] + kwargs["le"] = kwargs["maximum"] + return super().__new__(cls, **kwargs) + + @classmethod + def get_column_type(cls, **kwargs: Any) -> Any: + """ + Return proper type of db column for given field type. + Accepts required and optional parameters that each column type accepts. + + :param kwargs: key, value pairs of sqlalchemy options + :type kwargs: Any + :return: initialized column with proper options + :rtype: sqlalchemy Column + """ + return sqlalchemy.SmallInteger() + + class BigInteger(Integer, int): """ BigInteger field factory that construct Field classes and populated their values.