-
-
Save ape364/b3f5478853d2a409e3634891e821374d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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