diff --git a/docs/release-notes.md b/docs/release-notes.md index 1170ac0e..21c10bb3 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -17,22 +17,30 @@ hide: - Add `through_registry` to ManyToMany. - Add `no_copy` to models MetaInfo. - Add `ModelCollisionError` exception. +- Add keyword only hook function `real_add_to_registry`. It can be used to customize the `add_to_registry` behaviour. ### Changed - `create_edgy_model` has now `__type_kwargs__` which contains a dict of keyword arguments provided to `__new__` of type. - RelatedField uses now `no_copy`. - `add_to_registry` returns the type which was actually added to registry instead of None. -- Through models use now `no_copy` when autogenerated. -- BREAKING: instead of silent replacing models with the same `__name__` now an error is raised. +- Through models use now `no_copy` when autogenerated. This way they don't land in copied registries but are autogenerated again. +- Instead of silent replacing models with the same `__name__` now an error is raised. +- `skip_registry` has now also an allowed literal value: `"allow_search"`. It enables the search of the registry but doesn't register the model. ### Fixed - Copying registries and models is working now. - Fix deleting (clearing cache) of BaseForeignKey target. - Creating two models with the same name did lead to silent replacements. -- Invalidating causes schema errors. -- ManyToMany and ForeignKey fields in connection with tenancy. +- Invalidating caused schema errors. +- ManyToMany and ForeignKey fields didn't worked when referencing tenant models. +- ManyToMany fields didn't worked when specified on tenant models. + +### BREAKING + +- Instead of silent replacing models with the same `__name__` now an error is raised. +- The return value of `add_to_registry` changed. If you customize the function you need to return now the actual model added to the registry. ## 0.24.2 diff --git a/edgy/contrib/multi_tenancy/metaclasses.py b/edgy/contrib/multi_tenancy/metaclasses.py index 068f4e22..8e58d05a 100644 --- a/edgy/contrib/multi_tenancy/metaclasses.py +++ b/edgy/contrib/multi_tenancy/metaclasses.py @@ -51,12 +51,19 @@ def __new__( bases: tuple[type, ...], attrs: Any, on_conflict: Literal["error", "replace", "keep"] = "error", - skip_registry: bool = False, + skip_registry: Union[bool, Literal["allow_search"]] = False, + meta_info_class: type[TenantMeta] = TenantMeta, **kwargs: Any, ) -> Any: database: Union[Literal["keep"], None, Database, bool] = attrs.get("database", "keep") new_model = super().__new__( - cls, name, bases, attrs, meta_info_class=TenantMeta, skip_registry=True, **kwargs + cls, + name, + bases, + attrs, + skip_registry="allow_search", + meta_info_class=meta_info_class, + **kwargs, ) if new_model.meta.is_tenant is None: new_model.meta.is_tenant = _check_model_inherited_tenancy(bases) diff --git a/edgy/core/db/models/metaclasses.py b/edgy/core/db/models/metaclasses.py index 0df026ef..0bd83156 100644 --- a/edgy/core/db/models/metaclasses.py +++ b/edgy/core/db/models/metaclasses.py @@ -586,7 +586,7 @@ def __new__( bases: tuple[type, ...], attrs: dict[str, Any], meta_info_class: type[MetaInfo] = MetaInfo, - skip_registry: bool = False, + skip_registry: Union[bool, Literal["allow_search"]] = False, on_conflict: Literal["error", "replace", "keep"] = "error", **kwargs: Any, ) -> Any: @@ -784,10 +784,8 @@ def __new__( tablename = f"{name.lower()}s" meta.tablename = tablename meta.model = new_class - # Now find a registry and add it to the meta. This isn't affected by skip_registry. - # Use Meta: registry = False for disabling the search. - # The registry will be updated by add_to_registry. - if meta.registry is None: + # Now find a registry and add it to the meta. + if meta.registry is None and skip_registry is not True: registry: Union[Registry, None, Literal[False]] = get_model_registry(bases, meta_class) meta.registry = registry or None # don't add automatically to registry. Useful for subclasses which modify the registry itself. diff --git a/tests/metaclass/test_meta_errors.py b/tests/metaclass/test_meta_errors.py index a85a1a4f..5d20c1ba 100644 --- a/tests/metaclass/test_meta_errors.py +++ b/tests/metaclass/test_meta_errors.py @@ -135,12 +135,24 @@ class Meta: ) -def test_no_raises_ModelCollisionError(): - class User(edgy.StrictModel, skip_registry=True): +@pytest.mark.parametrize("value", [True, "allow_search"]) +def test_no_raises_ModelCollisionError_and_set_correctly(value): + class BaseUser(edgy.StrictModel): name = edgy.CharField(max_length=255) class Meta: registry = models + abstract = True + + class User(BaseUser, skip_registry=value): + pass + + if value is True: + assert BaseUser.meta.registry is models + assert User.meta.registry is None + else: + assert BaseUser.meta.registry is models + assert User.meta.registry is models def test_raises_ForeignKeyBadConfigured():