Skip to content

Commit

Permalink
More Meta class removal
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Feb 13, 2024
1 parent 6407697 commit 240006d
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 367 deletions.
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,39 +172,31 @@ import ormar
import sqlalchemy

DATABASE_URL = "sqlite:///db.sqlite"
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()


# note that this step is optional -> all ormar cares is a internal
# class with name Meta and proper parameters, but this way you do not
# have to repeat the same parameters if you use only one database
class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database
base_ormar_config = ormar.OrmarConfig(
database=databases.Database(DATABASE_URL),
metadata=sqlalchemy.MetaData(),
engine=sqlalchemy.create_engine(DATABASE_URL),
)


# Note that all type hints are optional
# below is a perfectly valid model declaration
# class Author(ormar.Model):
# class Meta(BaseMeta):
# tablename = "authors"
# ormar_config = base_ormar_config.copy()
#
# id = ormar.Integer(primary_key=True) # <= notice no field types
# name = ormar.String(max_length=100)


class Author(ormar.Model):
class Meta(BaseMeta):
tablename = "authors"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)


class Book(ormar.Model):
class Meta(BaseMeta):
tablename = "books"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
author: Optional[Author] = ormar.ForeignKey(Author)
Expand All @@ -215,10 +207,9 @@ class Book(ormar.Model):
# create the database
# note that in production you should use migrations
# note that this is not required if you connect to existing database
engine = sqlalchemy.create_engine(DATABASE_URL)
# just to be sure we clear the db before
metadata.drop_all(engine)
metadata.create_all(engine)
base_ormar_config.metadata.drop_all(engine)
base_ormar_config.metadata.create_all(engine)


# all functions below are divided into functionality categories
Expand Down
61 changes: 25 additions & 36 deletions docs/models/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ Since it can be a function you can set `default=datetime.datetime.now` and get c

```python
# <==related of code removed for clarity==>
base_ormar_config = ormar.OrmarConfig(
database=databases.Database(DATABASE_URL),
metadata=sqlalchemy.MetaData(),
engine=sqlalchemy.create_engine(DATABASE_URL),
)


class User(ormar.Model):
class Meta:
tablename: str = "users2"
metadata = metadata
database = database
ormar_config = base_ormar_config.copy(tablename="users2")

id: int = ormar.Integer(primary_key=True)
email: str = ormar.String(max_length=255, nullable=False)
Expand All @@ -90,7 +94,7 @@ class User(ormar.Model):
)

# <==related of code removed for clarity==>
app =FastAPI()
app = FastAPI()

@app.post("/users/")
async def create_user(user: User):
Expand Down Expand Up @@ -170,12 +174,7 @@ class MyQuerySetClass(QuerySet):


class Book(ormar.Model):

class Meta(ormar.ModelMeta):
metadata = metadata
database = database
tablename = "book"
queryset_class = MyQuerySetClass
ormar_config = base_ormar_config.copy(queryset_class=MyQuerySetClass)

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=32)
Expand Down Expand Up @@ -356,14 +355,13 @@ To ignore extra fields passed to `ormar` set this setting to `Extra.ignore` inst
Note that `ormar` does not allow accepting extra fields, you can only ignore them or forbid them (raise exception if present)

```python
from ormar import Extra
from ormar import Extra, OrmarConfig

class Child(ormar.Model):
class Meta(ormar.ModelMeta):
tablename = "children"
metadata = metadata
database = database
extra = Extra.ignore # set extra setting to prevent exceptions on extra fields presence
ormar_config = OrmarConfig(
tablename="children",
extra=Extra.ignore # set extra setting to prevent exceptions on extra fields presence
)

id: int = ormar.Integer(name="child_id", primary_key=True)
first_name: str = ormar.String(name="fname", max_length=100)
Expand All @@ -380,38 +378,29 @@ parameter to model `Meta` class.

Sample default ordering:
```python
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
base_ormar_config = ormar.OrmarConfig(
database=databases.Database(DATABASE_URL),
metadata=sqlalchemy.MetaData(),
)


class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database

# default sort by column id ascending
class Author(ormar.Model):
class Meta(BaseMeta):
tablename = "authors"
ormar_config = base_ormar_config.copy()

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
```
Modified
```python

database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()


class BaseMeta(ormar.ModelMeta):
metadata = metadata
database = database
base_ormar_config = ormar.OrmarConfig(
database=databases.Database(DATABASE_URL),
metadata=sqlalchemy.MetaData(),
)

# now default sort by name descending
class Author(ormar.Model):
class Meta(BaseMeta):
tablename = "authors"
orders_by = ["-name"]
ormar_config = base_ormar_config.copy(orders_by = ["-name"])

id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
Expand Down
Loading

0 comments on commit 240006d

Please sign in to comment.