Skip to content

Commit

Permalink
Remove Meta
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Feb 13, 2024
1 parent 240006d commit 9de0c65
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 81 deletions.
6 changes: 4 additions & 2 deletions docs/queries/select-columns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions docs/relations/foreign-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,15 @@ 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)
last_name: str = ormar.String(max_length=80)


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)
Expand Down
14 changes: 5 additions & 9 deletions docs/relations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 6 additions & 15 deletions docs/relations/many-to-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -170,20 +166,15 @@ 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)
courses = ormar.ManyToMany(Course)

# 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
Expand Down
31 changes: 8 additions & 23 deletions docs/relations/postponed-annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.
of how many relations are defined.
18 changes: 4 additions & 14 deletions docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,20 +513,15 @@ 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)
courses = ormar.ManyToMany(Course)

# 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
Expand All @@ -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)
Expand All @@ -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
Expand Down
22 changes: 8 additions & 14 deletions docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -85,4 +79,4 @@ async def sample_test():
async with database.transaction(force_rollback=True):
# your test code here
...
```
```

0 comments on commit 9de0c65

Please sign in to comment.