Skip to content

Instantly share code, notes, and snippets.

@blurrcat
Last active August 29, 2015 14:05
Show Gist options
  • Save blurrcat/1650d8960a2dffe6f3af to your computer and use it in GitHub Desktop.
Save blurrcat/1650d8960a2dffe6f3af to your computer and use it in GitHub Desktop.
Deferred database initialization for flask-peewee.db.Database
from flask import Flask
from models import Base
app = Flask('tets')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask_peewee.db import Database
from peewee import Model
class DeferredDatabase(Database):
def __init__(self, app=None):
if app:
super(DeferredDatabase, self).__init__(app)
else:
class BaseModel(Model):
pass
self.Model = BaseModel
def init_app(self, app):
self.app = app
self.load_database()
self.register_handlers()
class Meta:
database = self.database
self.Model.Meta = Meta
# database irrelevant model definitions
class User(Base):
class Meta:
db_table = 'test_user'
name = CharField()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment