Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should create one model class, just like Joomla did:</p> <pre><code>class User(Base): ... </code></pre> <p>And then you can use generic functionality of SQLAlchemy, and query for user you need,</p> <p>using primary key:</p> <pre><code>user = session.query(User).get(user_id) </code></pre> <p>or using unique username/email:</p> <pre><code>user = session.query(User).filter(User.username==my_username).first() </code></pre> <p>Speaking of user which is logged in for current HTTP request, web frameworks usually give you an instance automatically, passing it to HTTP request handler you're implementing. For example, Django framework gives you user instance from it's own ORM out of the box, as it has all authentication and session functionality in it. I also use Flask microframework with Flask-login module and SQLAlchemy, and there you need to implement a callback, which loads user instance by user Id, like I said before:</p> <pre><code>@login_manager.user_loader def load_user(userid): # just query for user return User.query.get(userid) # User.query is Flask-SQLAlchemy extension, it's same to session.query(User).get(userid) </code></pre> <p>Then you can get your user instance from a thread-local variable:</p> <pre><code>from flask.ext.login import login_required, current_user @app.route('/hello/') @login_required def my_request_handler(): return "&lt;html&gt;&lt;body&gt;Current user is %s&lt;/body&gt;&lt;/html&gt;" % current_user.username </code></pre> <p>Of course you can implement a static method, if you need a shortcut for getting user instance by Id:</p> <pre><code>class User(Base): ... @classmethod def by_id(cls, id): return session.query(cls).get(id) </code></pre> <p>(where SQLAlchemy's session is somehow defined globally)</p> <p>For Python/web apps architecture, you can look at <a href="https://djangoproject.com" rel="nofollow">Django</a> — it's very popular and easy to use all-in-one framework, and it has MVC-like architecture in it. Just <a href="https://docs.djangoproject.com/en/1.4/intro/tutorial01/" rel="nofollow">read the tutorial</a>.</p> <p>(But note that Django's own ORM's functionality is very limited in comparison to SQLAlchemy, if you'll wish to use Django).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload