Skip to content

Manager class

edgy.Manager

Manager(*, owner=None, inherit=True, name='', instance=None)

Bases: BaseManager

A concrete implementation of BaseManager, serving as the default manager for Edgy Models.

This manager provides the core functionality for querying model instances. Custom managers should inherit from this class to extend or modify query behavior.

Example
from edgy import Manager, Model


class MyCustomManager(Manager):
    # Custom manager logic can be added here
    pass


class MyOtherManager(Manager):
    # Another custom manager
    pass


class MyModel(Model):
    query = MyCustomManager()  # Assigning a custom manager
    active = MyOtherManager()  # Assigning another custom manager

    # ... model fields and other definitions

Initializes the BaseManager.

PARAMETER DESCRIPTION
owner

The owner model class associated with this manager. Defaults to None.

TYPE: type[BaseModelType] | None DEFAULT: None

inherit

A boolean indicating whether the manager should be inherited by subclasses. Defaults to True.

TYPE: bool DEFAULT: True

name

The name of the manager instance. Defaults to an empty string.

TYPE: str DEFAULT: ''

instance

The model instance associated with this manager, if it's an instance manager. Defaults to None.

TYPE: BaseModelType | None DEFAULT: None

Source code in edgy/core/db/models/managers.py
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
def __init__(
    self,
    *,
    owner: type[BaseModelType] | None = None,
    inherit: bool = True,
    name: str = "",
    instance: BaseModelType | None = None,
) -> None:
    """
    Initializes the BaseManager.

    Args:
        owner (type[BaseModelType] | None): The owner model class associated
                                              with this manager. Defaults to None.
        inherit (bool): A boolean indicating whether the manager should be
                        inherited by subclasses. Defaults to True.
        name (str): The name of the manager instance. Defaults to an empty string.
        instance (BaseModelType | None): The model instance associated with
                                         this manager, if it's an instance manager.
                                         Defaults to None.
    """
    self.owner = owner
    self.inherit = inherit
    self.name = name
    self.instance = instance

owner instance-attribute

owner = owner

inherit instance-attribute

inherit = inherit

name instance-attribute

name = name

instance instance-attribute

instance = instance

model_class property

model_class

Provides a legacy name for the owner property.

RETURNS DESCRIPTION
Any

The owner model class.

TYPE: Any

get_queryset

get_queryset()

Returns a QuerySet object tailored for the manager's owner model.

This method determines the appropriate schema and database to use for the queryset, considering whether the manager is bound to a model class or a specific instance.

RETURNS DESCRIPTION
QuerySet

An instance of QuerySet configured for the owner model.

TYPE: QuerySet

Source code in edgy/core/db/models/managers.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def get_queryset(self) -> QuerySet:
    """
    Returns a QuerySet object tailored for the manager's owner model.

    This method determines the appropriate schema and database to use for the queryset,
    considering whether the manager is bound to a model class or a specific instance.

    Returns:
        QuerySet: An instance of QuerySet configured for the owner model.
    """
    # Ensure that the owner is set before attempting to create a queryset.
    assert self.owner is not None

    # Determine the `using_schema` and `database` based on whether the manager
    # is associated with an instance or a class.
    if self.instance is not None:
        # If an instance is present, use its schema and database.
        using_schema = self.instance.__using_schema__
        database = self.instance.database
    else:
        # Otherwise, use the owner class's schema and database.
        using_schema = self.owner.__using_schema__
        database = self.owner.database

    # Return a new QuerySet initialized with the owner, schema, and database.
    return QuerySet(self.owner, using_schema=using_schema, database=database)