restore typing in tests and docs, remove unused metaclass code
This commit is contained in:
0
docs_src/models/__init__.py
Normal file
0
docs_src/models/__init__.py
Normal file
@ -12,6 +12,6 @@ class Course(ormar.Model):
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed= ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
@ -15,6 +15,6 @@ class Course(ormar.Model):
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed= ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
@ -12,9 +12,9 @@ class Course(ormar.Model):
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed= ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
|
||||
print(Course.__fields__)
|
||||
|
||||
@ -8,13 +8,13 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
class Meta:
|
||||
class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed= ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
|
||||
print(Course.Meta.table.columns)
|
||||
|
||||
@ -8,15 +8,16 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
class Meta:
|
||||
class Meta(ormar.ModelMeta):
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed= ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
print({x:v.__dict__ for x,v in Course.Meta.model_fields.items()})
|
||||
|
||||
print({x: v.__dict__ for x, v in Course.Meta.model_fields.items()})
|
||||
"""
|
||||
Will produce:
|
||||
{'completed': mappingproxy({'autoincrement': False,
|
||||
@ -63,4 +64,4 @@ Will produce:
|
||||
'server_default': None,
|
||||
'strip_whitespace': False,
|
||||
'unique': False})}
|
||||
"""
|
||||
"""
|
||||
|
||||
@ -14,8 +14,8 @@ class Course(ormar.Model):
|
||||
# define your constraints in Meta class of the model
|
||||
# it's a list that can contain multiple constraints
|
||||
# hera a combination of name and column will have to be unique in db
|
||||
constraints = [ormar.UniqueColumns('name', 'completed')]
|
||||
constraints = [ormar.UniqueColumns("name", "completed")]
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
@ -12,12 +12,11 @@ class Course(ormar.Model):
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
completed: bool = ormar.Boolean(default=False)
|
||||
|
||||
|
||||
course = Course(name="Painting for dummies", completed=False)
|
||||
await course.save()
|
||||
|
||||
await Course.objects.create(name="Painting for dummies", completed=False)
|
||||
await course.save() # type: ignore
|
||||
await Course.objects.create(name="Painting for dummies", completed=False) # type: ignore
|
||||
|
||||
@ -13,7 +13,7 @@ class Child(ormar.Model):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id = ormar.Integer(name='child_id', primary_key=True)
|
||||
first_name = ormar.String(name='fname', max_length=100)
|
||||
last_name = ormar.String(name='lname', max_length=100)
|
||||
born_year = ormar.Integer(name='year_born', nullable=True)
|
||||
id: int = ormar.Integer(name="child_id", primary_key=True)
|
||||
first_name: str = ormar.String(name="fname", max_length=100)
|
||||
last_name: str = ormar.String(name="lname", max_length=100)
|
||||
born_year: int = ormar.Integer(name="year_born", nullable=True)
|
||||
|
||||
@ -1,9 +1,21 @@
|
||||
from typing import Optional
|
||||
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from .docs010 import Artist # previous example
|
||||
|
||||
database = databases.Database("sqlite:///test.db", force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "music_albums"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id = ormar.Integer(name='album_id', primary_key=True)
|
||||
name = ormar.String(name='album_name', max_length=100)
|
||||
artist= ormar.ForeignKey(Artist, name='artist_id')
|
||||
id: int = ormar.Integer(name="album_id", primary_key=True)
|
||||
name: str = ormar.String(name="album_name", max_length=100)
|
||||
artist: Optional[Artist] = ormar.ForeignKey(Artist, name="artist_id")
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
from typing import Optional, Union, List
|
||||
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
from .docs008 import Child
|
||||
|
||||
database = databases.Database("sqlite:///test.db", force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class ArtistChildren(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "children_x_artists"
|
||||
@ -11,8 +23,10 @@ class Artist(ormar.Model):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
id = ormar.Integer(name='artist_id', primary_key=True)
|
||||
first_name = ormar.String(name='fname', max_length=100)
|
||||
last_name = ormar.String(name='lname', max_length=100)
|
||||
born_year = ormar.Integer(name='year')
|
||||
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(Child, through=ArtistChildren)
|
||||
id: int = ormar.Integer(name="artist_id", primary_key=True)
|
||||
first_name: str = ormar.String(name="fname", max_length=100)
|
||||
last_name: str = ormar.String(name="lname", max_length=100)
|
||||
born_year: int = ormar.Integer(name="year")
|
||||
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(
|
||||
Child, through=ArtistChildren
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user