Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8251443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:14:13+00:00 2026-06-08T00:14:13+00:00

I am coming from the PHP world, worked a lot with Joomla. Now I

  • 0

I am coming from the PHP world, worked a lot with Joomla. Now I am building a Python web application and I my first step is to create a user management when I faced a very basic question: How do you manage all users in one class, when your model represents only a single database row.

An example will make this more clear. Here’s what Joomla did:

JUser::getInstance($userid)

What this did was there is the class JUser and it has a static method getInstance() that operated on a static variable that was valid among all instances. In this way, JUser managed all Users while only representing one user as an instance of the class.

Now on Python I use SQLAlchemy and have a class like this:

class User(Base):

… and so on. Before I now start doing crappy stuff coming over from a crappy PHP world, I wanted to know: What is the correct and clean approach here? Build a new Users class that holds all users? Or is this static-method approach a good concept?

I have been googling for some time but I can’t seem to find something on this topic: Building larger applications in Python with the MV(C) model. Anyone have any good links on this I could read?

In a more specifc way: How do I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T00:14:15+00:00Added an answer on June 8, 2026 at 12:14 am

    You should create one model class, just like Joomla did:

    class User(Base):
        ...
    

    And then you can use generic functionality of SQLAlchemy, and query for user you need,

    using primary key:

    user = session.query(User).get(user_id)
    

    or using unique username/email:

    user = session.query(User).filter(User.username==my_username).first()
    

    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:

    @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)
    

    Then you can get your user instance from a thread-local variable:

    from flask.ext.login import login_required, current_user
    
    @app.route('/hello/')
    @login_required
    def my_request_handler():
        return "<html><body>Current user is %s</body></html>" % current_user.username
    

    Of course you can implement a static method, if you need a shortcut for getting user instance by Id:

    class User(Base):
        ...
    
        @classmethod
        def by_id(cls, id):
            return session.query(cls).get(id)
    

    (where SQLAlchemy’s session is somehow defined globally)

    For Python/web apps architecture, you can look at Django — it’s very popular and easy to use all-in-one framework, and it has MVC-like architecture in it. Just read the tutorial.

    (But note that Django’s own ORM’s functionality is very limited in comparison to SQLAlchemy, if you’ll wish to use Django).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Coming from PHP world, I used to create select box like this: <select> <?php
I'm new in the Python world, coming from PHP. So, this question may sound
I'm coming from Symfony2's world (PHP) and I'm trying to find a decent equivalent
I am coming from the world of PHP and am learning Ruby/Rails. I have
I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to
Coming from the world of HTML, XML and PHP its a new way of
I am somewhat new to qt/c++ (coming from a php/sql world) and I am
I'm new to django and am coming from the MAMP and PHP world. How
I'm new to Tomcat, servlets and Spring Web. I'm coming from a PHP background
Im coming from PHP world and im so confused about how to think when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.