On Oct 7, 9:46 am, Dan06 <[email protected]> wrote:
> I've recently started pylons, and I've put together some practice
> code. However, I'm unclear on how pylons models should be constructed
> and I'm unsure if I've correctly used sqlalchemy. My practice code has
> 2 relevant files, books_meta.py (model) and books.py (controller),
> which you'll find below. Based on my code, can anyone answer:
>
> 1. Have I setup the model correctly? I prefer to pre-build all my sql
> tables, therefore would/could I only need those five lines of code
> from 'books_meta.py' for all model usage?
> 2. Is the controller using the model correctly?
> 3. Does the sqlalchemy syntax and usage make sense?
>
> # books_meta.py file
> from sqlalchemy import schema, engine
>
> pg_db = engine.create_engine("postgres://
> user:[email protected]:
> 5432/sample")
> meta = schema.MetaData()
> meta.bind = pg_db
This looks mostly OK, but you typically only need one engine and
metadata per application, so instead of putting this into
books_meta.py, you probably want to put it into meta.py.
> #books.py file
> from library.model import books_meta
> from sqlalchemy import schema
>
> def index(self):
> books_tbl = schema.Table("books", books_meta.meta,
> autoload=True)
> publishers_tbl = schema.Table("publishers", books_meta.meta,
> autoload=True)
> c.books = books_tbl.select().execute()
> c.publishers = publishers_tbl.select().execute()
> return render("/library.html")
You don't want to create tables in a controller. Put your table
definitions in your model along side meta.py (model/book.py, model/
publisher.py, etc).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---