update docs part 1
This commit is contained in:
@ -8,9 +8,10 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
|
||||
@ -8,12 +8,13 @@ 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
|
||||
class Meta:
|
||||
# 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)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
|
||||
@ -8,26 +8,28 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
|
||||
print(Course.__pydantic_model__.__fields__)
|
||||
|
||||
print(Course.__fields__)
|
||||
"""
|
||||
Will produce:
|
||||
{'completed': ModelField(name='completed',
|
||||
type=bool,
|
||||
required=False,
|
||||
default=False),
|
||||
'id': ModelField(name='id',
|
||||
{'id': ModelField(name='id',
|
||||
type=Optional[int],
|
||||
required=False,
|
||||
default=None),
|
||||
'name': ModelField(name='name',
|
||||
type=Optional[str],
|
||||
required=False,
|
||||
default=None)}
|
||||
default=None),
|
||||
'completed': ModelField(name='completed',
|
||||
type=bool,
|
||||
required=False,
|
||||
default=False)}
|
||||
"""
|
||||
|
||||
@ -8,14 +8,16 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
|
||||
print(Course.__table__.columns)
|
||||
|
||||
print(Course.Meta.table.columns)
|
||||
"""
|
||||
Will produce:
|
||||
['courses.id', 'courses.name', 'courses.completed']
|
||||
|
||||
@ -8,44 +8,59 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
id: ormar.Integer(primary_key=True)
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
|
||||
print(Course.__model_fields__)
|
||||
print({x:v.__dict__ for x,v in Course.Meta.model_fields.items()})
|
||||
"""
|
||||
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}
|
||||
}
|
||||
"""
|
||||
{'completed': mappingproxy({'autoincrement': False,
|
||||
'choices': set(),
|
||||
'column_type': Boolean(),
|
||||
'default': False,
|
||||
'index': False,
|
||||
'name': 'completed',
|
||||
'nullable': True,
|
||||
'primary_key': False,
|
||||
'pydantic_only': False,
|
||||
'server_default': None,
|
||||
'unique': False}),
|
||||
'id': mappingproxy({'autoincrement': True,
|
||||
'choices': set(),
|
||||
'column_type': Integer(),
|
||||
'default': None,
|
||||
'ge': None,
|
||||
'index': False,
|
||||
'le': None,
|
||||
'maximum': None,
|
||||
'minimum': None,
|
||||
'multiple_of': None,
|
||||
'name': 'id',
|
||||
'nullable': False,
|
||||
'primary_key': True,
|
||||
'pydantic_only': False,
|
||||
'server_default': None,
|
||||
'unique': False}),
|
||||
'name': mappingproxy({'allow_blank': False,
|
||||
'autoincrement': False,
|
||||
'choices': set(),
|
||||
'column_type': String(length=100),
|
||||
'curtail_length': None,
|
||||
'default': None,
|
||||
'index': False,
|
||||
'max_length': 100,
|
||||
'min_length': None,
|
||||
'name': 'name',
|
||||
'nullable': False,
|
||||
'primary_key': False,
|
||||
'pydantic_only': False,
|
||||
'regex': None,
|
||||
'server_default': None,
|
||||
'strip_whitespace': False,
|
||||
'unique': False})}
|
||||
"""
|
||||
@ -1,41 +0,0 @@
|
||||
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 <- columns model is not stored on Course instance
|
||||
print(course.department)
|
||||
# Department(id=None, name='Science') <- Department model
|
||||
# returned from AliasManager
|
||||
print(course.department.name)
|
||||
# Science
|
||||
@ -8,11 +8,12 @@ metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class Course(ormar.Model):
|
||||
__database__ = database
|
||||
__metadata__ = metadata
|
||||
class Meta:
|
||||
database = database
|
||||
metadata = metadata
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(length=100)
|
||||
name = ormar.String(max_length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user