Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added init_app support. #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

added init_app support. #121

wants to merge 1 commit into from

Conversation

crvcrv
Copy link

@crvcrv crvcrv commented Jan 30, 2014

it can be extremely useful to initialize an empty Database and add the app in a different file.
currently it is not possible but with this fix it's possible to call
db = flask.ext.peewee.db.Database(None)
and later do
db.init_app(app)

def __init__(self, app):
def __init__(self, app=None):
if app:
self.init_db(app)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

init_db?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I pushed to wrong version. Going to create a new pull request for the fixed version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a new method init_app(app) to allow empty instantiation of Database objects:
db = Database(None)
db.init_app(app)
see http://pythonhosted.org/Flask-SQLAlchemy/api.html for details

@timster
Copy link

timster commented Apr 2, 2014

Just ran in to this issue today. I would love to see this become part of flask-peewee.

@AlJohri
Copy link

AlJohri commented Apr 4, 2014

+1

1 similar comment
@svenstaro
Copy link

+1

@JoeLanglois
Copy link

This is a must for any Flask extension, this needs to be in the master

@xtao
Copy link

xtao commented May 3, 2014

+1

1 similar comment
@irachex
Copy link

irachex commented Jul 4, 2014

+1

@Ppjet6
Copy link
Contributor

Ppjet6 commented Aug 17, 2014

I didn't see the issue, so I made my own version. I would love to see this patch accepted, not just for Database, but also for Admin and Auth.

https://github.com/Ppjet6/flask-peewee/tree/init_app
The thing with Admin and Auth is that I had to change the signatures. As I don't want to break everything I'm not requesting yet, I'll wait for someone with a better Idea.

@ketralnis
Copy link

This allows you to defer init_app but it doesn't actually give you separate state per application, since most of that state in peewee happens at import time (defined in the Meta child class). Importantly, one of the reasons that you want the factory pattern is so that you can override config settings in your unit tests, but this won't allow you to do that.

This would need coupling with a change to use peewee.Proxy for database initialisation

@RealJTG
Copy link

RealJTG commented Sep 17, 2014

Agree with @ketralnis. It should work unless you initialize db after your models (raise AttributeError while accessing db.Model) and may be used only as a workaround.

@Ppjet6
Copy link
Contributor

Ppjet6 commented Sep 18, 2014

@ketralnis, you're right. I did rewrite it with peewee.Proxy, and it works nicely.
I made a new pull request (#154)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.