diff --git a/ormar/models/metaclass.py b/ormar/models/metaclass.py index 88f5681..c9ac863 100644 --- a/ormar/models/metaclass.py +++ b/ormar/models/metaclass.py @@ -552,7 +552,13 @@ class ModelMetaclass(pydantic.main.ModelMetaclass): :param attrs: class namespace :type attrs: Dict """ - attrs["Config"] = get_pydantic_base_orm_config() + if "Config" in attrs: + class Config(attrs["Config"], get_pydantic_base_orm_config()): + pass + attrs["Config"] = Config + else: + attrs["Config"] = get_pydantic_base_orm_config() + attrs["__name__"] = name attrs, model_fields = extract_annotations_and_default_vals(attrs) for base in reversed(bases): diff --git a/tests/test_inheritance_and_pydantic_generation/test_inheritance_concrete.py b/tests/test_inheritance_and_pydantic_generation/test_inheritance_concrete.py index 41eac11..c655cf8 100644 --- a/tests/test_inheritance_and_pydantic_generation/test_inheritance_concrete.py +++ b/tests/test_inheritance_and_pydantic_generation/test_inheritance_concrete.py @@ -150,6 +150,12 @@ class Bus2(Car2): max_persons: int = ormar.Integer() +class ImmutablePerson(Person): + class Config: + allow_mutation = False + validate_assignment = False + + @pytest.fixture(autouse=True, scope="module") def create_test_database(): metadata.create_all(engine) @@ -495,3 +501,13 @@ async def test_inheritance_with_multi_relation(): assert len(unicorns) == 2 assert unicorns[1].name == "Unicorn 2" assert len(unicorns[1].co_owners) == 1 + + +def test_custom_config(): + # Custom config inherits defaults + assert getattr(ImmutablePerson.__config__, "orm_mode") == True + # Custom config can override defaults + assert getattr(ImmutablePerson.__config__, "validate_assignment") == False + sam = ImmutablePerson(name="Sam") + with pytest.raises(TypeError): + sam.name = "Not Sam"