Giter Club home page Giter Club logo

Comments (3)

coleifer avatar coleifer commented on August 16, 2024

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.

jamorton avatar jamorton commented on August 16, 2024

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.

coleifer avatar coleifer commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.