17

I am experimenting with Flask coming from Django and I really like it. There is just one problem that I ran into. I read the flask docs and the part about big applications or something like that and it explains a way to divide your project in packages, each one with its own static and templates folder as well as its own views module. the thing is that I cannot find a way that works to put the models in there using SQLAlchemy with the Flask extension. It works from the interactive prompt to create the tables, but when i use it inside the code it breaks. So I wanted to know how more experienced Flask developers solved this.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
actionAxolot
  • 485
  • 5
  • 16

2 Answers2

13

While I'm not ready to announce because I'm still actively working on refining the samples, you would probably benefit from the flask-skeleton project that I'm developing. I got tired of reinventing the wheel with regards to bootstrapping Flask websites so I started to a complete sample project that uses my best practices. I haven't added any unit tests yet, but this should be good enough for you to start with. Please send me feedback or suggestions if you come across any.

https://github.com/sean-/flask-skeleton/

Sean
  • 9,888
  • 4
  • 40
  • 43
  • Great thanks for the link. But is all of that bootstraping necessary in recent versions of Flask? just asking. I got everything working separated into modules and it works. The minute I add a model it breaks. Perhaps I want to Django it a lot. At the top of the model import db from the main app and it spews back the error "cannot import name db in 'name of file' " What could be happening here? – actionAxolot May 22 '11 at 21:32
  • You could be running in to a circular import (that's the reason I do my model imports after the app has been created). Flask-SQLAlchemy uses LocalProxy extensively to help with some of this, too. – Sean May 23 '11 at 01:20
  • Sean, i've looked through your skeleton and it is a good solution to this problem. however its very heavy for most cases. and it doesn't implement two key features. 1. It needs to bake in the HTML 5 boilerplate 2. It uses modules exclusively, no use of blueprints – Jay Jan 10 '12 at 22:31
  • Correct on all accounts, this is a pre-0.7 example and was meant to not be a simple "hello world" blog post application. I have an updated version that I need to sit down and push and may in the next week or so. Check the Flask mailing list for another post from me for a lighter weight example. – Sean Jan 12 '12 at 20:09
5

Actually I found out what I was looking for. Instead of importing flaskext.sqlalchemy on the main __init__ you import it in the model. After that you import the model in the main __init__ and with db.init_app() start it and pass the app configurations. It is not as flexible as the skeleton shown in @Sean post, but it was what I wanted to know. If i weren't toying around probably the skeleton would be the one I'd use.

unmounted
  • 33,530
  • 16
  • 61
  • 61
actionAxolot
  • 485
  • 5
  • 16
  • When I initially started using Flask, the whole "bootstrapping the database models" was less intuitive than I would have liked, but that was largely due to me making direct access to SQLAlchemy instead of Flask-SQLAlchemy. Recreating the wheel finally got to me and that's why cobbled together the skeleton. – Sean May 23 '11 at 01:26
  • Yeah it is actually awesome! In fact I am going to use it in another project I have. It was just one of those programming itches you have in which you really want to know how to do it a certain way. I found out how and it is actually useless in the real world but I had fun doing it. – actionAxolot May 23 '11 at 01:41
  • Glad to hear. I've spent the better part of the weekend toiling away on the login/registration and session code, which is getting a major overhaul in the skeleton. I'm a bit appalled at how bad session management still is... going to check out Beaker before I reinvent that wheel. Anyway, FYI. – Sean May 23 '11 at 02:00
  • Great! Following the skeleton repo on github so I'll have a look once it changes. And perhaps as you asked for a little feedback on it and perhaps some other form of participation. – actionAxolot May 23 '11 at 05:24