django-tortoise-objects 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,18 @@
1
+ """
2
+ django-tortoise-objects: Bridge Django ORM models to Tortoise ORM for truly async database access.
3
+
4
+ This package provides automatic generation of Tortoise ORM model mirrors from
5
+ Django models, enabling async database access via the ``tortoise_objects``
6
+ descriptor on Django model classes.
7
+
8
+ Public API
9
+ ----------
10
+ - ``init()`` -- Explicitly initialize Tortoise ORM connections.
11
+ - ``close()`` -- Shut down Tortoise ORM connections.
12
+ - ``get_tortoise_model()`` -- Retrieve the generated Tortoise model for a Django model.
13
+ """
14
+
15
+ from django_tortoise.initialization import close, init
16
+ from django_tortoise.registry import get_tortoise_model
17
+
18
+ __all__ = ["close", "get_tortoise_model", "init"]
@@ -0,0 +1,16 @@
1
+ """
2
+ Module that holds the ``__models__`` list for Tortoise ORM model discovery.
3
+
4
+ When Tortoise ``init()`` scans this module, it will use the ``__models__``
5
+ list instead of trying to discover ``Model`` subclasses via module inspection.
6
+ The list is populated during ``AppConfig.ready()`` by the model generator.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import TYPE_CHECKING
12
+
13
+ if TYPE_CHECKING:
14
+ from tortoise import models
15
+
16
+ __models__: list[type[models.Model]] = []
@@ -0,0 +1,106 @@
1
+ """
2
+ Django AppConfig for django-tortoise-objects.
3
+
4
+ Registers Tortoise model mirrors during Django's ``ready()`` phase.
5
+ Performs a two-pass model generation: first pass creates data-field-only
6
+ models, second pass adds relational fields after all targets are registered.
7
+ """
8
+
9
+ import logging
10
+
11
+ from django.apps import AppConfig, apps
12
+
13
+ logger = logging.getLogger("django_tortoise")
14
+
15
+
16
+ class DjangoTortoiseConfig(AppConfig):
17
+ name = "django_tortoise"
18
+ verbose_name = "Django Tortoise Objects"
19
+ default_auto_field = "django.db.models.BigAutoField"
20
+
21
+ def ready(self):
22
+ """
23
+ Synchronous startup: introspect Django models and generate Tortoise
24
+ counterparts. Does NOT establish any database connections.
25
+
26
+ Uses a two-pass approach:
27
+ Pass 1 -- Introspect all included Django models, pre-register their
28
+ Tortoise class names so FK string references resolve.
29
+ Pass 2 -- Generate Tortoise models with ALL fields (data + relational)
30
+ now that all target class names are known.
31
+ """
32
+ from django_tortoise import _models
33
+ from django_tortoise.conf import get_config, should_include
34
+ from django_tortoise.generator import generate_tortoise_model_full
35
+ from django_tortoise.introspection import ModelInfo, introspect_model, should_skip_model
36
+ from django_tortoise.manager import TortoiseObjects
37
+ from django_tortoise.registry import model_registry
38
+
39
+ config = get_config()
40
+ include_patterns = config.get("INCLUDE_MODELS")
41
+ exclude_patterns = config.get("EXCLUDE_MODELS")
42
+
43
+ all_models = apps.get_models()
44
+ generated_count = 0
45
+ skipped_count = 0
46
+
47
+ # Pass 1: Introspect and pre-register class name mappings
48
+ # This lets FK string references resolve in Pass 2.
49
+ eligible: list[tuple[type, str, ModelInfo]] = [] # (django_model, label, model_info)
50
+ class_name_map: dict[type, str] = {} # django_model -> tortoise class name
51
+
52
+ for django_model in all_models:
53
+ label = f"{django_model._meta.app_label}.{django_model.__name__}"
54
+
55
+ if not should_include(label, include_patterns, exclude_patterns):
56
+ logger.debug("Model '%s' excluded by configuration.", label)
57
+ skipped_count += 1
58
+ continue
59
+
60
+ model_info = introspect_model(django_model)
61
+
62
+ skip, reason = should_skip_model(model_info)
63
+ if skip:
64
+ logger.debug("Skipping model '%s': %s", label, reason)
65
+ skipped_count += 1
66
+ continue
67
+
68
+ tortoise_class_name = f"{django_model.__name__}Tortoise"
69
+ class_name_map[django_model] = tortoise_class_name
70
+ eligible.append((django_model, label, model_info))
71
+
72
+ # Pass 2: Generate Tortoise models with ALL fields
73
+ for django_model, label, model_info in eligible:
74
+ tortoise_model = generate_tortoise_model_full(model_info, class_name_map=class_name_map)
75
+ if tortoise_model is None:
76
+ skipped_count += 1
77
+ continue
78
+
79
+ model_registry.register(django_model, tortoise_model, label)
80
+ _models.__models__.append(tortoise_model)
81
+ django_model.tortoise_objects = TortoiseObjects(tortoise_model) # type: ignore[attr-defined]
82
+ generated_count += 1
83
+
84
+ logger.info(
85
+ "django-tortoise-objects: Generated %d Tortoise models, skipped %d.",
86
+ generated_count,
87
+ skipped_count,
88
+ )
89
+
90
+
91
+ # Backward-compatible alias: import from conf.should_include
92
+ def _should_include(
93
+ label: str,
94
+ include_patterns: list[str] | None,
95
+ exclude_patterns: list[str] | None,
96
+ ) -> bool:
97
+ """
98
+ Check if a model label matches include/exclude patterns.
99
+
100
+ .. deprecated::
101
+ Use ``django_tortoise.conf.should_include()`` instead.
102
+ This alias is kept for backward compatibility with existing tests.
103
+ """
104
+ from django_tortoise.conf import should_include
105
+
106
+ return should_include(label, include_patterns, exclude_patterns)