Skip to content

BaseField class

edgy.core.db.fields.base.BaseField

BaseField(*, default=Undefined, title=None, description=None, **kwargs)

Bases: FieldInfo, Representation

The base field for all Edgy data model fields.

PARAMETER DESCRIPTION
default

TYPE: Any DEFAULT: Undefined

title

TYPE: Optional[str] DEFAULT: None

description

TYPE: Optional[str] DEFAULT: None

**kwargs

TYPE: Any DEFAULT: {}

Source code in edgy/core/db/fields/base.py
 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
 94
 95
 96
 97
 98
 99
100
101
def __init__(
    self,
    *,
    default: Any = Undefined,
    title: Optional[str] = None,
    description: Optional[str] = None,
    **kwargs: Any,
) -> None:
    self.max_digits: str = kwargs.pop("max_digits", None)
    self.decimal_places: str = kwargs.pop("decimal_places", None)
    self.server_default: Any = kwargs.pop("server_default", None)

    super().__init__(**kwargs)

    self.null: bool = kwargs.pop("null", False)
    if self.null and default is Undefined:
        default = None
    if default is not Undefined:
        self.default = default
    if (
        (default is not None)
        and default != Undefined
        or (self.server_default is not None and self.server_default != Undefined)
    ):
        self.null = True

    self.defaulf_factory: Optional[Callable[..., Any]] = kwargs.pop(
        "defaulf_factory", Undefined
    )
    self.field_type: Any = kwargs.pop("__type__", None)
    self.__original_type__: type = kwargs.pop("__original_type__", None)
    self.primary_key: bool = kwargs.pop("primary_key", False)
    self.column_type: sqlalchemy.Column = kwargs.pop("column_type", None)
    self.constraints: Sequence[sqlalchemy.Constraint] = kwargs.pop("constraints", None)
    self.title = title
    self.description = description
    self.read_only: bool = kwargs.pop("read_only", False)
    self.help_text: str = kwargs.pop("help_text", None)
    self.pattern: Pattern = kwargs.pop("pattern", None)
    self.autoincrement: bool = kwargs.pop("autoincrement", False)
    self.related_name: str = kwargs.pop("related_name", None)
    self.unique: bool = kwargs.pop("unique", False)
    self.index: bool = kwargs.pop("index", False)
    self.choices: Sequence = kwargs.pop("choices", None)
    self.owner: Any = kwargs.pop("owner", None)
    self.name: str = kwargs.pop("name", None)
    self.alias: str = kwargs.pop("name", None)
    self.regex: str = kwargs.pop("regex", None)
    self.format: str = kwargs.pop("format", None)
    self.min_length: Optional[Union[int, float, decimal.Decimal]] = kwargs.pop(
        "min_length", None
    )
    self.max_length: Optional[Union[int, float, decimal.Decimal]] = kwargs.pop(
        "max_length", None
    )
    self.minimum: Optional[Union[int, float, decimal.Decimal]] = kwargs.pop("minimum", None)
    self.maximum: Optional[Union[int, float, decimal.Decimal]] = kwargs.pop("maximum", None)
    self.multiple_of: Optional[Union[int, float, decimal.Decimal]] = kwargs.pop(
        "multiple_of", None
    )
    self.through: Any = kwargs.pop("through", None)
    self.server_onupdate: Any = kwargs.pop("server_onupdate", None)
    self.registry: Registry = kwargs.pop("registry", None)
    self.comment: str = kwargs.pop("comment", None)
    self.secret: bool = kwargs.pop("secret", False)

    if self.primary_key:
        default_value = default
        self.raise_for_non_default(default=default_value, server_default=self.server_default)

    for name, value in kwargs.items():
        edgy_setattr(self, name, value)

    if self.primary_key:
        self.field_type = Any
        self.null = True

    if isinstance(self.default, bool):
        self.null = True
    self.__namespace__ = {k: v for k, v in self.__dict__.items() if k != "__namespace__"}

max_digits instance-attribute

max_digits = pop('max_digits', None)

decimal_places instance-attribute

decimal_places = pop('decimal_places', None)

server_default instance-attribute

server_default = pop('server_default', None)

null instance-attribute

null = pop('null', False)

default instance-attribute

default = default

defaulf_factory instance-attribute

defaulf_factory = pop('defaulf_factory', Undefined)

field_type instance-attribute

field_type = pop('__type__', None)

__original_type__ instance-attribute

__original_type__ = pop('__original_type__', None)

primary_key instance-attribute

primary_key = pop('primary_key', False)

column_type instance-attribute

column_type = pop('column_type', None)

constraints instance-attribute

constraints = pop('constraints', None)

title instance-attribute

title = title

description instance-attribute

description = description

read_only instance-attribute

read_only = pop('read_only', False)

help_text instance-attribute

help_text = pop('help_text', None)

pattern instance-attribute

pattern = pop('pattern', None)

autoincrement instance-attribute

autoincrement = pop('autoincrement', False)

related_name instance-attribute

related_name = pop('related_name', None)

unique instance-attribute

unique = pop('unique', False)

index instance-attribute

index = pop('index', False)

choices instance-attribute

choices = pop('choices', None)

owner instance-attribute

owner = pop('owner', None)

name instance-attribute

name = pop('name', None)

alias instance-attribute

alias = pop('name', None)

regex instance-attribute

regex = pop('regex', None)

format instance-attribute

format = pop('format', None)

min_length instance-attribute

min_length = pop('min_length', None)

max_length instance-attribute

max_length = pop('max_length', None)

minimum instance-attribute

minimum = pop('minimum', None)

maximum instance-attribute

maximum = pop('maximum', None)

multiple_of instance-attribute

multiple_of = pop('multiple_of', None)

through instance-attribute

through = pop('through', None)

server_onupdate instance-attribute

server_onupdate = pop('server_onupdate', None)

registry instance-attribute

registry = pop('registry', None)

comment instance-attribute

comment = pop('comment', None)

secret instance-attribute

secret = pop('secret', False)

__namespace__ class-attribute instance-attribute

__namespace__ = {k: _0for (k, v) in items() if k != '__namespace__'}

namespace property

namespace

Returns the properties added to the fields in a dict format

is_required

is_required()

Check if the argument is required.

RETURNS DESCRIPTION
bool

True if the argument is required, False otherwise.

Source code in edgy/core/db/fields/base.py
108
109
110
111
112
113
114
115
def is_required(self) -> bool:
    """Check if the argument is required.

    Returns:
        `True` if the argument is required, `False` otherwise.
    """
    required = False if self.null else True
    return bool(required and not self.primary_key)

raise_for_non_default

raise_for_non_default(default, server_default)
PARAMETER DESCRIPTION
default

TYPE: Any

server_default

TYPE: Any

Source code in edgy/core/db/fields/base.py
117
118
119
120
121
122
123
124
125
126
127
128
129
def raise_for_non_default(self, default: Any, server_default: Any) -> Any:
    has_default: bool = True
    has_server_default: bool = True

    if default is None or default is False:
        has_default = False
    if server_default is None or server_default is False:
        has_server_default = False

    if not self.field_type == int and not has_default and not has_server_default:
        raise FieldDefinitionError(
            "Primary keys other then IntegerField and BigIntegerField, must provide a default or a server_default."
        )

get_alias

get_alias()

Used to translate the model column names into database column tables.

Source code in edgy/core/db/fields/base.py
131
132
133
134
135
def get_alias(self) -> str:
    """
    Used to translate the model column names into database column tables.
    """
    return self.name

is_primary_key

is_primary_key()

Sets the autoincrement to True if the field is primary key.

Source code in edgy/core/db/fields/base.py
137
138
139
140
141
142
143
def is_primary_key(self) -> bool:
    """
    Sets the autoincrement to True if the field is primary key.
    """
    if self.primary_key:
        self.autoincrement = True
    return False

has_default

has_default()

Checks if the field has a default value set

Source code in edgy/core/db/fields/base.py
145
146
147
def has_default(self) -> bool:
    """Checks if the field has a default value set"""
    return bool(self.default is not None and self.default is not Undefined)

get_column

get_column(name)

Returns the column type of the field being declared.

PARAMETER DESCRIPTION
name

TYPE: str

Source code in edgy/core/db/fields/base.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def get_column(self, name: str) -> Any:
    """
    Returns the column type of the field being declared.
    """
    if self.column_type == sqlalchemy.ForeignKey:
        return self.get_column(name)

    constraints = self.get_constraints()
    return sqlalchemy.Column(
        name,
        self.column_type,
        *constraints,
        primary_key=self.primary_key,
        nullable=self.null and not self.primary_key,
        index=self.index,
        unique=self.unique,
        default=self.default,
        comment=self.comment,
        server_default=self.server_default,
        server_onupdate=self.server_onupdate,
    )

expand_relationship

expand_relationship(value)

Used to be overritten by any Link class.

PARAMETER DESCRIPTION
value

TYPE: Any

Source code in edgy/core/db/fields/base.py
171
172
173
174
175
def expand_relationship(self, value: Any) -> Any:
    """
    Used to be overritten by any Link class.
    """
    return value
get_related_name()

Returns the related name used for reverse relations

Source code in edgy/core/db/fields/base.py
177
178
179
def get_related_name(self) -> str:
    """Returns the related name used for reverse relations"""
    return self.related_name

get_constraints

get_constraints()
Source code in edgy/core/db/fields/base.py
181
182
def get_constraints(self) -> Any:
    return self.constraints

get_default_value

get_default_value()
Source code in edgy/core/db/fields/base.py
184
185
186
187
188
def get_default_value(self) -> Any:
    default = getattr(self, "default", None)
    if callable(default):
        return default()
    return default