Comments (3)
Thanks for bringing this up - the need to have a database instance at model definition time is ... well, it is what it is. You brought up django -- django declares the database(s) in a global (and much maligned) settings object. One of the decisions I made with peewee was to make it not need global database configuration. One question is, if you don't declare your database at class creation time, what time is appropriate?
I'm not sure if this will help you, but my general workflow looks like this (to avoid circular imports):
- app.py
- flask application instantiation
- peewee database instantiation
- create a "BaseModel" class that uses the above database
- models.py or whatever
- import the BaseModel from app.py and extend for any specific models I need
- views.py
- import the flask app from app.py to create blueprint/url rules/etc
- import any models needed from app.py
- main.py
- import the flask app from app.py
- import all the views so they get registered
- run the flask app or whatever
from peewee.
I think that database selection should be be able to take place any time before a connection needs to be made, so in the case of flask, sometime before @app.before_request ideally. For example, instead of the model classes holding a pointer to a database object, they hold a pointer to a callable that will give them a database object when they're instantiated. Or, both are permitted so the simple case doesn't change.
I understand that there are reasonable ways to do everything in the global scope - and maybe that's the pythonic way - but it seemed weird when I had to move a lot of my stuff from classes into toplevel code to get it working.
from peewee.
Cool, also bear in mind that there's a difference between the database object (what you use with models) and the underlying connection/when that connection is made. It is made when it is first needed if not explicitly created. Also, you may want to check out flask-peewee, which does some of the things you've mentioned.
https://github.com/coleifer/flask-peewee/tree/master/example
from peewee.
Related Issues (20)
- Regression of < used on connections HOT 4
- Correct way to migrate a table to update a ForeignKeyConstraint with `ON DELETE CASCADE` HOT 1
- the count is diffrent HOT 3
- Bind schema - similar to how we can bind database to a model HOT 2
- Subquery inside CASE...WHEN... should not have alias added HOT 2
- Deleting items from table with JOIN in a query HOT 5
- TypeError: 'str' object is not callable peewee HOT 1
- Handle connection in Sanic framework HOT 3
- pwiz generates invalid Python code when creating BareField instances HOT 5
- Reuse Mysql Connection Exception HOT 2
- Joining From Different Database throwing an error HOT 1
- create_tables() does not create any fields HOT 2
- pyproject.toml file can't build peewee HOT 5
- Unexpected argument 'named_cursor' after upgrade to 3.17.6 HOT 3
- bulk update uuid PostgreSQL HOT 1
- Check Constraint not recognized in Pewee but in SQLITE3 HOT 4
- 可不可以支持开发sqlserver? HOT 2
- sql in record update HOT 6
- Database as single source of truth HOT 4
- When joining two tables using a foreign key, the query result for the foreign key field does not meet expectations.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peewee.