restore typing in tests and docs, remove unused metaclass code

This commit is contained in:
collerek
2020-11-01 10:11:25 +01:00
parent be35c80412
commit 358b5c2e52
49 changed files with 354 additions and 324 deletions

0
docs_src/__init__.py Normal file
View File

View File

View File

@ -32,8 +32,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Item(ormar.Model):
@ -42,9 +42,9 @@ class Item(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
category = ormar.ForeignKey(Category, nullable=True)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
category: Optional[Category] = ormar.ForeignKey(Category, nullable=True)
@app.get("/items/", response_model=List[Item])

View File

View File

@ -29,8 +29,8 @@ class Course(ormar.Model):
department: Optional[Department] = ormar.ForeignKey(Department)
department = Department(name='Science')
course = Course(name='Math', completed=False, department=department)
department = Department(name="Science")
course = Course(name="Math", completed=False, department=department)
print(department.courses[0])
# Will produce:

View File

@ -14,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Course(ormar.Model):
@ -23,14 +23,14 @@ 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)
department= ormar.ForeignKey(Department, related_name="my_courses")
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Department] = ormar.ForeignKey(Department, related_name="my_courses")
department = Department(name='Science')
course = Course(name='Math', completed=False, department=department)
department = Department(name="Science")
course = Course(name="Math", completed=False, department=department)
print(department.my_courses[0])
# Will produce:

View File

@ -14,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Course(ormar.Model):
@ -23,7 +23,7 @@ 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)
department= ormar.ForeignKey(Department)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Department] = ormar.ForeignKey(Department)

View File

@ -16,8 +16,8 @@ class Product(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
company = ormar.String(max_length=200, server_default='Acme')
sort_order = ormar.Integer(server_default=text("10"))
created= ormar.DateTime(server_default=func.now())
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
company: str = ormar.String(max_length=200, server_default="Acme")
sort_order: int = ormar.Integer(server_default=text("10"))
created: datetime = ormar.DateTime(server_default=func.now())

View File

View 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)

View File

@ -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)

View File

@ -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__)

View File

@ -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)

View File

@ -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})}
"""
"""

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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")

View File

@ -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
)

View File

View File

@ -14,8 +14,8 @@ class Album(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Track(ormar.Model):
@ -24,7 +24,7 @@ class Track(ormar.Model):
metadata = metadata
database = database
id = ormar.Integer(primary_key=True)
album= ormar.ForeignKey(Album)
title = ormar.String(max_length=100)
position = ormar.Integer()
id: int = ormar.Integer(primary_key=True)
album: Optional[Album] = ormar.ForeignKey(Album)
title: str = ormar.String(max_length=100)
position: int = ormar.Integer()

View File

View File

@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Dict, Union
import databases
import sqlalchemy
@ -14,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
class Course(ormar.Model):
@ -23,22 +23,22 @@ 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)
department= ormar.ForeignKey(Department)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
department = Department(name='Science')
department = Department(name="Science")
# set up a relation with actual Model instance
course = Course(name='Math', completed=False, department=department)
course = Course(name="Math", completed=False, department=department)
# set up relation with only related model pk value
course2 = Course(name='Math II', completed=False, department=department.pk)
course2 = Course(name="Math II", completed=False, department=department.pk)
# set up a relation with dictionary corresponding to related model
course3 = Course(name='Math III', completed=False, department=department.dict())
course3 = Course(name="Math III", completed=False, department=department.dict())
# explicitly set up None
course4 = Course(name='Math III', completed=False, department=None)
course4 = Course(name="Math III", completed=False, department=None)

View File

@ -14,9 +14,9 @@ class Author(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
first_name = ormar.String(max_length=80)
last_name = ormar.String(max_length=80)
id: int = ormar.Integer(primary_key=True)
first_name: str = ormar.String(max_length=80)
last_name: str = ormar.String(max_length=80)
class Category(ormar.Model):
@ -25,8 +25,8 @@ class Category(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=40)
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=40)
class PostCategory(ormar.Model):
@ -44,7 +44,9 @@ class Post(ormar.Model):
database = database
metadata = metadata
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(Category, through=PostCategory)
author= ormar.ForeignKey(Author)
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(
Category, through=PostCategory
)
author: Optional[Author] = ormar.ForeignKey(Author)