diff --git a/docs/queries/select-columns.md b/docs/queries/select-columns.md index c687ee0d9..ad32a01ee 100644 --- a/docs/queries/select-columns.md +++ b/docs/queries/select-columns.md @@ -202,8 +202,10 @@ import sqlalchemy import ormar from tests.settings import DATABASE_URL -database = databases.Database(DATABASE_URL, force_rollback=True) -metadata = sqlalchemy.MetaData() +base_ormar_config = ormar.OrmarConfig( + database=databases.Database(DATABASE_URL, force_rollback=True), + metadata=sqlalchemy.MetaData(), +) class Company(ormar.Model): diff --git a/docs/relations/foreign-key.md b/docs/relations/foreign-key.md index 4e84ffb55..5860ad2dc 100644 --- a/docs/relations/foreign-key.md +++ b/docs/relations/foreign-key.md @@ -52,8 +52,7 @@ Example: ```python class Author(ormar.Model): - class Meta(BaseMeta): - pass + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) first_name: str = ormar.String(max_length=80) @@ -61,8 +60,7 @@ class Author(ormar.Model): class Post(ormar.Model): - class Meta(BaseMeta): - pass + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) diff --git a/docs/relations/index.md b/docs/relations/index.md index eaa539675..60065e23a 100644 --- a/docs/relations/index.md +++ b/docs/relations/index.md @@ -24,13 +24,11 @@ To define many-to-one relation use `ForeignKey` field. The definition of one-to-many relation also uses `ForeignKey`, and it's registered for you automatically. -So in relation ato example above. +So in relation to example above. -```Python hl_lines="9" +```Python hl_lines="8" class Department(ormar.Model): - class Meta: - database = database - metadata = metadata + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -194,14 +192,12 @@ In order to create auto-relation or create two models that reference each other different relations (remember the reverse side is auto-registered for you), you need to use `ForwardRef` from `typing` module. -```python hl_lines="1 11 14" +```python hl_lines="1 9 12" PersonRef = ForwardRef("Person") class Person(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) diff --git a/docs/relations/many-to-many.md b/docs/relations/many-to-many.md index c9d6ee443..c1d3746f6 100644 --- a/docs/relations/many-to-many.md +++ b/docs/relations/many-to-many.md @@ -28,16 +28,14 @@ By default it's child (source) `Model` name + s, like courses in snippet below: ```python class Category(ormar.Model): - class Meta(BaseMeta): - tablename = "categories" + ormar_config = base_ormar_config.copy(tablename="categories") id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=40) class Post(ormar.Model): - class Meta(BaseMeta): - pass + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) @@ -96,16 +94,14 @@ Example: ```python class Category(ormar.Model): - class Meta(BaseMeta): - tablename = "categories" + ormar_config = base_ormar_config.copy(tablename="categories") id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=40) class Post(ormar.Model): - class Meta(BaseMeta): - pass + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) @@ -170,9 +166,7 @@ So in example like this: ```python ... # course declaration omitted class Student(ormar.Model): - class Meta: - database = database - metadata = metadata + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -180,10 +174,7 @@ class Student(ormar.Model): # will produce default Through model like follows (example simplified) class StudentCourse(ormar.Model): - class Meta: - database = database - metadata = metadata - tablename = "students_courses" + ormar_config = base_ormar_config.copy(tablename="students_courses") id: int = ormar.Integer(primary_key=True) student = ormar.ForeignKey(Student) # default name diff --git a/docs/relations/postponed-annotations.md b/docs/relations/postponed-annotations.md index e15629611..5907f8a34 100644 --- a/docs/relations/postponed-annotations.md +++ b/docs/relations/postponed-annotations.md @@ -36,9 +36,7 @@ PersonRef = ForwardRef("Person") class Person(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -72,9 +70,7 @@ PersonRef = ForwardRef("Person") class Person(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -93,14 +89,10 @@ and through parameters. ChildRef = ForwardRef("Child") class ChildFriend(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() class Child(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -132,9 +124,7 @@ TeacherRef = ForwardRef("Teacher") class Student(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -144,16 +134,11 @@ class Student(ormar.Model): class StudentTeacher(ormar.Model): - class Meta(ModelMeta): - tablename = 'students_x_teachers' - metadata = metadata - database = db + ormar_config = base_ormar_config.copy(tablename='students_x_teachers') class Teacher(ormar.Model): - class Meta(ModelMeta): - metadata = metadata - database = db + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -168,4 +153,4 @@ Student.update_forward_refs() !!!warning Remember that `related_name` needs to be unique across related models regardless - of how many relations are defined. \ No newline at end of file + of how many relations are defined. diff --git a/docs/releases.md b/docs/releases.md index bc2e393a1..7b3a416a2 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -513,9 +513,7 @@ In 0.10.9 ormar excludes versions with vulnerability in pinned dependencies. ```python ... # course declaration ommited class Student(ormar.Model): - class Meta: - database = database - metadata = metadata + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -523,10 +521,7 @@ In 0.10.9 ormar excludes versions with vulnerability in pinned dependencies. # will produce default Through model like follows (example simplified) class StudentCourse(ormar.Model): - class Meta: - database = database - metadata = metadata - tablename = "students_courses" + ormar_config = base_ormar_config.copy(tablename="students_courses") id: int = ormar.Integer(primary_key=True) student = ormar.ForeignKey(Student) # default name @@ -540,9 +535,7 @@ In 0.10.9 ormar excludes versions with vulnerability in pinned dependencies. ```python ... # course declaration ommited class Student(ormar.Model): - class Meta: - database = database - metadata = metadata + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) @@ -552,10 +545,7 @@ In 0.10.9 ormar excludes versions with vulnerability in pinned dependencies. # will produce default Through model like follows (example simplified) class StudentCourse(ormar.Model): - class Meta: - database = database - metadata = metadata - tablename = "students_courses" + ormar_config = base_ormar_config.copy(tablename="students_courses") id: int = ormar.Integer(primary_key=True) student_id = ormar.ForeignKey(Student) # set by through_relation_name diff --git a/docs/transactions.md b/docs/transactions.md index 2b8a904a2..4f4415cd9 100644 --- a/docs/transactions.md +++ b/docs/transactions.md @@ -26,24 +26,18 @@ import databases import sqlalchemy import ormar -metadata = sqlalchemy.MetaData() -database = databases.Database("sqlite:///") + +base_ormar_config = OrmarConfig( + metadata=sqlalchemy.MetaData(), + database = databases.Database("sqlite:///"), +) + class Author(ormar.Model): - class Meta: - database=database - metadata=metadata + ormar_config = base_ormar_config.copy() id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=255) - -# database is accessible from class -database = Author.Meta.database - -# as well as from instance -author = Author(name="Stephen King") -database = author.Meta.database - ``` You can also use `.transaction()` as a function decorator on any async function: @@ -85,4 +79,4 @@ async def sample_test(): async with database.transaction(force_rollback=True): # your test code here ... -``` \ No newline at end of file +```