Skip to content

Commit

Permalink
Replace more Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Feb 19, 2024
1 parent 6ec0849 commit 7d28e87
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/models/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
24 changes: 12 additions & 12 deletions docs/models/inheritance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
```
Expand Down Expand Up @@ -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': <class 'ormar.fields.model_fields.Integer'>,
'name': <class 'ormar.fields.model_fields.String'>,
Expand Down Expand Up @@ -300,7 +300,7 @@ class Bus(Car):
Now the columns looks much better.

```python
Person.Meta.model_fields
Person.ormar_config.model_fields
"""
{'id': <class 'ormar.fields.model_fields.Integer'>,
'name': <class 'ormar.fields.model_fields.String'>,
Expand Down Expand Up @@ -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
Expand All @@ -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
<class 'abc.PersonsCarBus2'>
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
<class 'abc.PersonsCarTruck2'>
Truck2.Meta.model_fields['co_owners'].through.Meta.tablename
Truck2.ormar_config.model_fields['co_owners'].through.ormar_config.tablename
'cars_x_persons_trucks2'
```

Expand All @@ -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'])
```
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions docs/models/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand Down
10 changes: 4 additions & 6 deletions docs/models/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions docs/models/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7d28e87

Please sign in to comment.