introduce docs -> models section mostly finished
This commit is contained in:
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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(orm.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 = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Department(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
|
||||
|
||||
class Course(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.Boolean(default=False)
|
||||
department = orm.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 orm
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(orm.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
completed = orm.Boolean(default=False)
|
||||
|
||||
|
||||
course = Course(name="Painting for dummies", completed=False)
|
||||
await course.save()
|
||||
|
||||
await Course.objects.create(name="Painting for dummies", completed=False)
|
||||
Reference in New Issue
Block a user