finish fields docs intial ver,add test for related name, fix child_name(s) in reverse relations
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 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(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 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, 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 orm
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(orm.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
name = orm.String(length=100)
|
||||
|
||||
|
||||
class Track(orm.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
|
||||
id = orm.Integer(primary_key=True)
|
||||
album = orm.ForeignKey(Album, nullable=False)
|
||||
title = orm.String(length=100)
|
||||
position = orm.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
|
||||
Reference in New Issue
Block a user