From 7d28e87d0ae9434ae91ab3642efe37bc5fba9570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Goran=20Meki=C4=87?= Date: Mon, 19 Feb 2024 14:27:11 +0100 Subject: [PATCH] Replace more Meta --- docs/models/index.md | 2 +- docs/models/inheritance.md | 24 ++++++++++++------------ docs/models/internals.md | 6 +++--- docs/models/methods.md | 10 ++++------ docs/models/migrations.md | 4 ++-- docs/transactions.md | 2 +- 6 files changed, 23 insertions(+), 25 deletions(-) diff --git a/docs/models/index.md b/docs/models/index.md index fa723eb5a..bb8d7db1e 100644 --- a/docs/models/index.md +++ b/docs/models/index.md @@ -344,7 +344,7 @@ If you try to do so the `ModelError` will be raised. Since the extra fields cannot be saved in the database the default to disallow such fields seems a feasible option. -On the contrary in `pydantic` the default option is to ignore such extra fields, therefore `ormar` provides an `Meta.extra` setting to behave in the same way. +On the contrary in `pydantic` the default option is to ignore such extra fields, therefore `ormar` provides an `ormar_config.extra` setting to behave in the same way. To ignore extra fields passed to `ormar` set this setting to `Extra.ignore` instead of default `Extra.forbid`. diff --git a/docs/models/inheritance.md b/docs/models/inheritance.md index d1b874ab5..6207acf95 100644 --- a/docs/models/inheritance.md +++ b/docs/models/inheritance.md @@ -175,12 +175,12 @@ class RedefinedField(DateFieldsModel): # you can verify that the final field is correctly declared and created -changed_field = RedefinedField.Meta.model_fields["created_date"] +changed_field = RedefinedField.ormar_config.model_fields["created_date"] assert changed_field.default is None assert changed_field.alias == "creation_date" -assert any(x.name == "creation_date" for x in RedefinedField.Meta.table.columns) +assert any(x.name == "creation_date" for x in RedefinedField.ormar_config.table.columns) assert isinstance( - RedefinedField.Meta.table.columns["creation_date"].type, + RedefinedField.ormar_config.table.columns["creation_date"].type, sqlalchemy.sql.sqltypes.String, ) ``` @@ -263,7 +263,7 @@ class Bus(Car): Now when you will inspect the fields on Person model you will get: ```python -Person.Meta.model_fields +Person.ormar_config.model_fields """ {'id': , 'name': , @@ -300,7 +300,7 @@ class Bus(Car): Now the columns looks much better. ```python -Person.Meta.model_fields +Person.ormar_config.model_fields """ {'id': , 'name': , @@ -386,7 +386,7 @@ That way for class Truck2 the relation defined in You can verify the names by inspecting the list of fields present on `Person` model. ```python -Person.Meta.model_fields +Person.ormar_config.model_fields { # note how all relation fields need to be unique on Person # regardless if autogenerated or manually overwritten @@ -409,14 +409,14 @@ But that's not all. It's kind of internal to `ormar` but affects the data struct so let's examine the through models for both `Bus2` and `Truck2` models. ```python -Bus2.Meta.model_fields['co_owners'].through +Bus2.ormar_config.model_fields['co_owners'].through -Bus2.Meta.model_fields['co_owners'].through.Meta.tablename +Bus2.ormar_config.model_fields['co_owners'].through.ormar_config.tablename 'cars_x_persons_buses2' -Truck2.Meta.model_fields['co_owners'].through +Truck2.ormar_config.model_fields['co_owners'].through -Truck2.Meta.model_fields['co_owners'].through.Meta.tablename +Truck2.ormar_config.model_fields['co_owners'].through.ormar_config.tablename 'cars_x_persons_trucks2' ``` @@ -427,7 +427,7 @@ the name of the **table** from the child is used. Note that original model is not only not used, the table for this model is removed from metadata: ```python -Bus2.Meta.metadata.tables.keys() +Bus2.ormar_config.metadata.tables.keys() dict_keys(['test_date_models', 'categories', 'subjects', 'persons', 'trucks', 'buses', 'cars_x_persons_trucks2', 'trucks2', 'cars_x_persons_buses2', 'buses2']) ``` @@ -453,7 +453,7 @@ Ormar allows you to skip certain fields in inherited model that are coming from !!!Note Note that the same behaviour can be achieved by splitting the model into more abstract models and mixins - which is a preferred way in normal circumstances. -To skip certain fields from a child model, list all fields that you want to skip in `model.Meta.exclude_parent_fields` parameter like follows: +To skip certain fields from a child model, list all fields that you want to skip in `model.ormar_config.exclude_parent_fields` parameter like follows: ```python base_ormar_config = OrmarConfig( diff --git a/docs/models/internals.md b/docs/models/internals.md index defce7a43..358c31957 100644 --- a/docs/models/internals.md +++ b/docs/models/internals.md @@ -20,7 +20,7 @@ For example to list pydantic model fields you can: ## Sqlalchemy Table -To access auto created sqlalchemy table you can use `Model.Meta.table` parameter +To access auto created sqlalchemy table you can use `Model.ormar_config.table` parameter For example to list table columns you can: @@ -29,14 +29,14 @@ For example to list table columns you can: ``` !!!tip - You can access table primary key name by `Course.Meta.pkname` + You can access table primary key name by `Course.ormar_config.pkname` !!!info For more options visit official [sqlalchemy-metadata][sqlalchemy-metadata] documentation. ## Fields Definition -To access ormar `Fields` you can use `Model.Meta.model_fields` parameter +To access ormar `Fields` you can use `Model.ormar_config.model_fields` parameter For example to list table model fields you can: diff --git a/docs/models/methods.md b/docs/models/methods.md index db0b464c0..2b6cf5eba 100644 --- a/docs/models/methods.md +++ b/docs/models/methods.md @@ -294,14 +294,12 @@ That means that this way you can effortlessly create pydantic models for request Given sample ormar models like follows: ```python -metadata = sqlalchemy.MetaData() -database = databases.Database(DATABASE_URL, force_rollback=True) +base_ormar_config = ormar.OrmarConfig( + metadata=sqlalchemy.MetaData(), + database=databases.Database(DATABASE_URL, force_rollback=True), +) -class BaseMeta(ormar.ModelMeta): - metadata = metadata - database = database - class Category(ormar.Model): ormar_config = base_ormar_config.copy(tablename="categories") diff --git a/docs/models/migrations.md b/docs/models/migrations.md index 684101456..7577a682e 100644 --- a/docs/models/migrations.md +++ b/docs/models/migrations.md @@ -16,14 +16,14 @@ engine = sqlalchemy.create_engine("sqlite:///test.db") metadata.create_all(engine) ``` -You can also create single tables, sqlalchemy tables are exposed in `ormar.Meta` class. +You can also create single tables, sqlalchemy tables are exposed in `ormar.ormar_config` object. ```python import sqlalchemy # get your database url in sqlalchemy format - same as used with databases instance used in Model definition engine = sqlalchemy.create_engine("sqlite:///test.db") # Artist is an ormar model from previous examples -Artist.Meta.table.create(engine) +Artist.ormar_config.table.create(engine) ``` !!!warning diff --git a/docs/transactions.md b/docs/transactions.md index 3c8dba49a..1ad5ae0a8 100644 --- a/docs/transactions.md +++ b/docs/transactions.md @@ -18,7 +18,7 @@ async with database.transaction(): Note that it has to be the same `database` that the one used in Model's `ormar_config` object. To avoid passing `database` instance around in your code you can extract the instance from each `Model`. -Database provided during declaration of `ormar.Model` is available through `Meta.database` and can +Database provided during declaration of `ormar.Model` is available through `ormar_config.database` and can be reached from both class and instance. ```python