update docs part 2
This commit is contained in:
@ -1,26 +1,42 @@
|
||||
import ormar
|
||||
import databases
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
class Department(ormar.Model):
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
|
||||
|
||||
class Track(ormar.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
class Course(ormar.Model):
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
album = ormar.ForeignKey(Album)
|
||||
title = ormar.String(length=100)
|
||||
position = ormar.Integer()
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
department: ormar.ForeignKey(Department)
|
||||
|
||||
|
||||
department = Department(name='Science')
|
||||
|
||||
# set up a relation with actual Model instance
|
||||
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)
|
||||
|
||||
# set up a relation with dictionary corresponding to related model
|
||||
course3 = Course(name='Math III', completed=False, department=department.dict())
|
||||
|
||||
# explicitly set up None
|
||||
course4 = Course(name='Math III', completed=False, department=None)
|
||||
|
||||
@ -1,39 +1,48 @@
|
||||
import ormar
|
||||
import databases
|
||||
import ormar
|
||||
import sqlalchemy
|
||||
|
||||
database = databases.Database("sqlite:///db.sqlite")
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Album(ormar.Model):
|
||||
__tablename__ = "album"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
class Author(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "authors"
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
first_name: ormar.String(max_length=80)
|
||||
last_name: ormar.String(max_length=80)
|
||||
|
||||
|
||||
class Track(ormar.Model):
|
||||
__tablename__ = "track"
|
||||
__metadata__ = metadata
|
||||
__database__ = database
|
||||
class Category(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "categories"
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
album = ormar.ForeignKey(Album)
|
||||
title = ormar.String(length=100)
|
||||
position = ormar.Integer()
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=40)
|
||||
|
||||
|
||||
print(Track.__table__.columns['album'].__repr__())
|
||||
# Will produce:
|
||||
# Column('album', Integer(), ForeignKey('album.id'), table=<track>)
|
||||
class PostCategory(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "posts_categories"
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
print(Track.__pydantic_model__.__fields__['album'])
|
||||
# Will produce:
|
||||
# ModelField(
|
||||
# name='album'
|
||||
# type=Optional[Album]
|
||||
# required=False
|
||||
# default=None)
|
||||
# If there are no additional columns id will be created automatically as Integer
|
||||
|
||||
|
||||
class Post(ormar.Model):
|
||||
class Meta:
|
||||
tablename = "posts"
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id: ormar.Integer(primary_key=True)
|
||||
title: ormar.String(max_length=200)
|
||||
categories: ormar.ManyToMany(Category, through=PostCategory)
|
||||
author: ormar.ForeignKey(Author)
|
||||
|
||||
@ -1,44 +0,0 @@
|
||||
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