Skip to content

Instantly share code, notes, and snippets.

@ape364
Created February 26, 2019 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ape364/b3f5478853d2a409e3634891e821374d to your computer and use it in GitHub Desktop.
Save ape364/b3f5478853d2a409e3634891e821374d to your computer and use it in GitHub Desktop.
import logging
from tortoise import Tortoise, run_async, Model, fields
logging.basicConfig(
level=logging.DEBUG,
format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
)
logger = logging.getLogger(__name__)
async def init_db():
await Tortoise.init(
{
'connections': {
'default': {
'engine': 'tortoise.backends.asyncpg',
'credentials': {
'host': 'localhost',
'port': 5432,
'user': 'postgres',
'password': 'postgres',
'database': 'testdb',
}
}
},
'apps': {
'models': {
'models': ['__main__'],
'default_connection': 'default',
}
}
}
)
await Tortoise.generate_schemas()
class AbstractModel(Model):
id = fields.IntField(pk=True)
created = fields.DatetimeField(auto_now_add=True)
updated = fields.DatetimeField(auto_now=True)
class Meta:
abstract = True
class Something(AbstractModel):
name = fields.TextField()
def __str__(self):
return str(self.name)
if __name__ == '__main__':
run_async(init_db())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment