version with pydantic inheritance passing all the tests
This commit is contained in:
36
docs_src/fields/docs001.py
Normal file
36
docs_src/fields/docs001.py
Normal file
@ -0,0 +1,36 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Department(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
department = ormar.ForeignKey(Department)
|
||||
|
||||
|
||||
department = Department(name='Science')
|
||||
course = Course(name='Math', completed=False, department=department)
|
||||
|
||||
print(department.courses[0])
|
||||
# Will produce:
|
||||
# Course(id=None,
|
||||
# name='Math',
|
||||
# completed=False,
|
||||
# department=Department(id=None, name='Science'))
|
||||
36
docs_src/fields/docs002.py
Normal file
36
docs_src/fields/docs002.py
Normal file
@ -0,0 +1,36 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Department(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
department = ormar.ForeignKey(Department, related_name="my_courses")
|
||||
|
||||
department = Department(name='Science')
|
||||
course = Course(name='Math', completed=False, department=department)
|
||||
|
||||
print(department.my_courses[0])
|
||||
# Will produce:
|
||||
# Course(id=None,
|
||||
# name='Math',
|
||||
# completed=False,
|
||||
# department=Department(id=None, name='Science'))
|
||||
|
||||
41
docs_src/fields/docs003.py
Normal file
41
docs_src/fields/docs003.py
Normal file
@ -0,0 +1,41 @@
|
||||
import ormar
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Track(ormar.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
album = ormar.ForeignKey(Album, nullable=False)
|
||||
title = ormar.String(length=100)
|
||||
position = ormar.Integer()
|
||||
|
||||
|
||||
album = await Album.objects.create(name="Brooklyn")
|
||||
await Track.objects.create(album=album, title="The Bird", position=1)
|
||||
|
||||
# explicit preload of related Album Model
|
||||
track = await Track.objects.select_related("album").get(title="The Bird")
|
||||
assert track.album.name == 'Brooklyn'
|
||||
# Will produce: True
|
||||
|
||||
# even without explicit select_related if ForeignKey is not nullable,
|
||||
# the Album Model is still preloaded.
|
||||
track2 = await Track.objects.get(title="The Bird")
|
||||
assert track2.album.name == 'Brooklyn'
|
||||
# Will produce: True
|
||||
16
docs_src/models/docs001.py
Normal file
16
docs_src/models/docs001.py
Normal file
@ -0,0 +1,16 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
19
docs_src/models/docs002.py
Normal file
19
docs_src/models/docs002.py
Normal file
@ -0,0 +1,19 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
# if you omit this parameter it will be created automatically
|
||||
# as class.__name__.lower()+'s' -> "courses" in this example
|
||||
__tablename__ = "my_courses"
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
33
docs_src/models/docs003.py
Normal file
33
docs_src/models/docs003.py
Normal file
@ -0,0 +1,33 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
|
||||
print(Course.__pydantic_model__.__fields__)
|
||||
"""
|
||||
Will produce:
|
||||
{'completed': ModelField(name='completed',
|
||||
type=bool,
|
||||
required=False,
|
||||
default=False),
|
||||
'id': ModelField(name='id',
|
||||
type=Optional[int],
|
||||
required=False,
|
||||
default=None),
|
||||
'name': ModelField(name='name',
|
||||
type=Optional[str],
|
||||
required=False,
|
||||
default=None)}
|
||||
"""
|
||||
22
docs_src/models/docs004.py
Normal file
22
docs_src/models/docs004.py
Normal file
@ -0,0 +1,22 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
|
||||
print(Course.__table__.columns)
|
||||
"""
|
||||
Will produce:
|
||||
['courses.id', 'courses.name', 'courses.completed']
|
||||
"""
|
||||
51
docs_src/models/docs005.py
Normal file
51
docs_src/models/docs005.py
Normal file
@ -0,0 +1,51 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
|
||||
print(Course.__model_fields__)
|
||||
"""
|
||||
Will produce:
|
||||
{
|
||||
'id': {'name': 'id',
|
||||
'primary_key': True,
|
||||
'autoincrement': True,
|
||||
'nullable': False,
|
||||
'default': None,
|
||||
'server_default': None,
|
||||
'index': None,
|
||||
'unique': None,
|
||||
'pydantic_only': False},
|
||||
'name': {'name': 'name',
|
||||
'primary_key': False,
|
||||
'autoincrement': False,
|
||||
'nullable': True,
|
||||
'default': None,
|
||||
'server_default': None,
|
||||
'index': None,
|
||||
'unique': None,
|
||||
'pydantic_only': False,
|
||||
'length': 100},
|
||||
'completed': {'name': 'completed',
|
||||
'primary_key': False,
|
||||
'autoincrement': False,
|
||||
'nullable': True,
|
||||
'default': False,
|
||||
'server_default': None,
|
||||
'index': None,
|
||||
'unique': None,
|
||||
'pydantic_only': False}
|
||||
}
|
||||
"""
|
||||
41
docs_src/models/docs006.py
Normal file
41
docs_src/models/docs006.py
Normal file
@ -0,0 +1,41 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Department(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
department = ormar.ForeignKey(Department)
|
||||
|
||||
|
||||
department = Department(name="Science")
|
||||
course = Course(name="Math", completed=False, department=department)
|
||||
|
||||
print('name' in course.__dict__)
|
||||
# False <- property name is not stored on Course instance
|
||||
print(course.name)
|
||||
# Math <- value returned from underlying pydantic model
|
||||
print('department' in course.__dict__)
|
||||
# False <- related model is not stored on Course instance
|
||||
print(course.department)
|
||||
# Department(id=None, name='Science') <- Department model
|
||||
# returned from RelationshipManager
|
||||
print(course.department.name)
|
||||
# Science
|
||||
22
docs_src/models/docs007.py
Normal file
22
docs_src/models/docs007.py
Normal file
@ -0,0 +1,22 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = 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)
|
||||
26
docs_src/relations/docs001.py
Normal file
26
docs_src/relations/docs001.py
Normal file
@ -0,0 +1,26 @@
|
||||
import ormar
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Track(ormar.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
album = ormar.ForeignKey(Album)
|
||||
title = ormar.String(length=100)
|
||||
position = ormar.Integer()
|
||||
39
docs_src/relations/docs002.py
Normal file
39
docs_src/relations/docs002.py
Normal file
@ -0,0 +1,39 @@
|
||||
import ormar
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Track(ormar.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
album = ormar.ForeignKey(Album)
|
||||
title = ormar.String(length=100)
|
||||
position = ormar.Integer()
|
||||
|
||||
|
||||
print(Track.__table__.columns['album'].__repr__())
|
||||
# Will produce:
|
||||
# Column('album', Integer(), ForeignKey('album.id'), table=<track>)
|
||||
|
||||
print(Track.__pydantic_model__.__fields__['album'])
|
||||
# Will produce:
|
||||
# ModelField(
|
||||
# name='album'
|
||||
# type=Optional[Album]
|
||||
# required=False
|
||||
# default=None)
|
||||
44
docs_src/relations/docs003.py
Normal file
44
docs_src/relations/docs003.py
Normal file
@ -0,0 +1,44 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class SchoolClass(ormar.Model):
|
||||
__tablename__ = "schoolclasses"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Category(ormar.Model):
|
||||
__tablename__ = "categories"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
|
||||
|
||||
class Student(ormar.Model):
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
schoolclass = ormar.ForeignKey(SchoolClass)
|
||||
category = ormar.ForeignKey(Category)
|
||||
|
||||
|
||||
class Teacher(ormar.Model):
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
schoolclass = ormar.ForeignKey(SchoolClass)
|
||||
category = ormar.ForeignKey(Category)
|
||||
Reference in New Issue
Block a user