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 819677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:21:00+00:00 2026-05-15T02:21:00+00:00

Python noob here, Currently I’m working with SQLAlchemy, and I have this: from __init__

  • 0

Python noob here,

Currently I’m working with SQLAlchemy, and I have this:

from __init__ import Base
from sqlalchemy.schema import Column, ForeignKey
from sqlalchemy.types import Integer, String
from sqlalchemy.orm import relationship

class User(Base):
    __tablename__ = "users"
    id = Column(Integer, primary_key=True)
    username = Column(String, unique=True)
    email = Column(String)
    password = Column(String)
    salt = Column(String)
    openids = relationship("OpenID", backref="users")

User.__table__.create(checkfirst=True)

#snip definition of OpenID class

def create(**kwargs):
    user = User()
    if "username" in kwargs.keys():
        user.username = kwargs['username']
    if "email" in kwargs.keys():
        user.username = kwargs['email']
    if "password" in kwargs.keys():
        user.password = kwargs['password']

    return user

This is in /db/users.py, so it would be used like:

from db import users
new_user = users.create(username="Carson", password="1234")
new_user.email = "email@address.com"
users.add(new_user) #this function obviously not defined yet

but the code in create() is a little stupid, and I’m wondering if there’s a better way to do it that doesn’t require an if ladder, and that will fail if any keys are added that aren’t in the User object already. Like:

for attribute in kwargs.keys():
    if attribute in User:
        setattr(user, attribute, kwargs[attribute])
    else:
        raise Exception("blah")

that way I could put this in its own function (unless one hopefully already exists?) So I wouldn’t have to do the if ladder again and again, and so I could change the table structure without modifying this code.

Any suggestions?

  • 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-05-15T02:21:02+00:00Added an answer on May 15, 2026 at 2:21 am

    My suggestion would be to not simplify it any further. You risk stepping on important object structures if you assign arbitrary attributes.

    The one simplification I would do is to drop .keys() when you use it on a dict; both containment checking and iteration already use the keys.

    …

    On second thought, you could have a class attribute that contains known safe attributes, and then check this attribute within the function, and use setattr() on the instance.

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

Sidebar

Related Questions

this may sound like a strange question from a Python noob, but here's the
this is my models.py from django.db import models # Create your models here. class
I am a noob to Python and have not had any luck figuring this
This is the what-I-want ( warning: Python noob here, this might be an incorrect
Python beginner here, I have a list of lists and want to refer to
Python noob using 2.7 on Windows. I'm working on making a hierarchy tree view
complete Python noob here (and rusty in C). I am using a Mac with
I keep getting an error whenever I enter this code (I'm a python noob
Python noob; please explain why this loop doesn't exit. for i in range(0,10): print
Using python to pick it some pieces so definitely a noob ? here but

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.