Extensions
Miggy has extension modules which are collected under the miggy.ext namespace.
Enum fields
Miggy provides classes for working with enum fields in
Peewee models. These fields are intended to simplify integration with
enum.StrEnum and enum.IntEnum.
It is important to understand that enum fields do not create native ENUM types in the database. They are thin wrappers around standard Peewee fields:
CharEnumField— a wrapper aroundCharFieldIntEnumField— a wrapper aroundSmallIntegerField
Values are stored in the database as plain strings or integers.
Example:
import enum
from peewee import Model
from miggy.ext import CharEnumField, IntEnumField
class Status(enum.StrEnum):
NEW = "new"
IN_PROGRESS = "in_progress"
DONE = "done"
class Priority(enum.IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
class Task(Model):
status = CharEnumField(Status, max_length=32)
priority = IntEnumField(Priority)
Migrations
Enum fields are fully supported by Miggy migrations.
When migrations are generated, these fields will appear as their underlying Peewee field types:
CharEnumField→CharFieldIntegerEnumField→SmallIntegerField
API
- class miggy.ext.fields.BaseEnumField(enum: type[Enum], **kwargs: Any)[source]
Base field for working with Enum values
Model Factory
model_factory is a simple helper to create dynamic model instances for testing purposes.
- miggy.ext.model_factory(custom_field_type_map: dict[type[Field], Callable[[Field], Any]] | None = None, fill_nullable_values: bool = False, **kwargs: Any) Model[source]
Create and save an instance of a Peewee model, automatically populating all required fields.
Parameters
- modeltype[pw.Model]
The Peewee model class to instantiate.
- custom_field_type_mapFieldMap | None, optional
A mapping that allows customizing factory functions for specific field types. If None, default handlers are used.
- fill_nullable_valuesbool, optional
Whether to automatically fill nullable fields as well.
- kwargsAny
field_name=value pairs that override or provide values for particular model fields.
Returns
- pw.Model
The newly created and saved model instance.
Example
>>> book = model_factory(Book, name="mytestname")