introduce docs -> models section mostly finished
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user