这里的Lav noob
目前,Im与Kalkere合作,我有:
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
见/db/users.py
,因此将使用:
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
但是,<代码>create()中的代码是一个微小的 st,我很想知道,是否有更好的方式来做到这一点,这是否要求阶梯,如果在用户物体中添加任何标记,就会失败。 和:
for attribute in kwargs.keys():
if attribute in User:
setattr(user, attribute, kwargs[attribute])
else:
raise Exception("blah")
这样,我就能够把这一点放在自己的职能中(除非有希望已经存在吗?) 因此,我不得不再一次地做一下,这样,我就可以改变表格结构,而不修改这一法典。
任何建议?