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

  • Home
  • SEARCH
  • 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 8344435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:22:21+00:00 2026-06-09T06:22:21+00:00

I use SQLAlchemy to define a User class. if an user is not logged

  • 0

I use SQLAlchemy to define a User class.

if an user is not logged on my website, I want to identify him as an AnonymousUser. I want my AnonymousUser to be a subclass of my normal User class, with special method (eg. is_anonymous(self): return true).

Now, there is an anonymous user that I created initially in my database, with (say) id = 0.

I would like to define AnonymousUser as a subclass of User, where the constructor only returns the anonymous user :

class AnonymousUser(User):
    def __init__(self):
        anonymous_user = User.get_by_id(0)
        self.username = anonymous_user.username
        self.id = anonymous_user.id
        ...

but when I create an AnonymousUser, it does not understand that I don’t want a new User, but only to create an existing User, and it commits the creation of a new User to the session, and since the id has been set to the existing anonymous_user id it crashes because of the id unicity condition

How can I tell SQLAlchemy to create a User object using an existing id?

Is there a more elegant way to do what I want?

I know that I could simply define an anonymous_user by using User.get_by_id(0), but then I would have to add an is_anonymous field to my object and have a variable check each time I check whether my user is anonymous (rather than having two distinct classes, one with a trivial implementation is_anonymous: return False and the other using is_anonymous: return True

  • 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-09T06:22:22+00:00Added an answer on June 9, 2026 at 6:22 am

    A quick suggestion. You can implement your is_anonymous method on the User class like this:

    def is_anonymous(self):
        return self.id == 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use sqlalchemy with the pyramid framework, and i want to link a person
I am working with SQLAlchemy, and I'm not yet sure which database I'll use
Why do people use SQLAlchemy instead of MySQLdb? What advantages does it offer?
Trying to find out if it's possible to use SQLAlchemy on Heroku. Thanks.
table : id (integer primary key) data (blob) I use mysql and sqlalchemy. To
SQLAlchemy seems really heavyweight if all I use is MySQL. Why are convincing reasons
Use Case Show a photo uploaded by the user in a square box with
use C#,want to upload excel file on google doc. bellow syntax use to upload
Related (for the no-association-object use case): SQLAlchemy Many-to-Many Relationship on a Single Table Building
I'm writing an Fast-CGI application that makes use of sqlAlchemy & MySQL for persistent

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.