Python TypeError:为参数获取了多个值

时间:2015-10-10 23:01:35

标签: python postgresql sqlalchemy

我想要实现的目标

我正在尝试在我的项目中设置Flask RESTful Web服务。 我将所有数据存储在PostgreSQL数据库中。在我的项目中,我使用SQLAlchemy为我的表创建数据模型。

我创建了一些抽象模型来为我的所有模型添加特定属性。 请参阅下面的代码。

问题

我有一个代表系统User(UserModel)的模型。当我想创建一个新用户时,我收到以下错误:

  

TypeError: __ init __()为参数'user_type'获取了多个值

我的所作所为:

user = UserModel("user@test.com", "test", "user", UserType.admin)

我不明白为什么它说'user_type'参数在我传递4个参数时获得多个值。 init函数有5个,包括self属性。

我怀疑抽象类,但我不确定。当我删除user_type参数和属性时,我收到此错误:

  

TypeError: __ init __()需要4个位置参数,但有5个被赋予

usermodel.py

from core.enums.usertypes import UserType
from core.models.base.auditeddeletemodel import db, ForeignKey, AuditedDeletable
from core.models.addressmodel import AddressModel


class UserModel(AuditedDeletable):
    """A model which contains information about an User.

    Attributes:
        emailaddress (string): The emailaddress for the User.
        first_name (string): The first name of the User.
        last_name (string): The last name of the User.
        user_type (UserType): The type of the User (default: viewer).
    """
    __tablename__ = 'Users'

    emailaddress = db.Column(db.String(100))
    first_name = db.Column(db.String(100))
    last_name = db.Column(db.String(100))
    date_of_birth = db.Column(db.DateTime)
    gender = db.Column(db.String(1))
    password_hash = db.Column(db.String(100))
    password_salt = db.Column(db.String(100))
    user_type_id = db.Column(db.Integer)
    mobile_phone_number = db.Column(db.String(30))
    home_phone_number = db.Column(db.String(30))
    address_id = db.Column(db.Integer, ForeignKey('Addresses.id'))
    address = db.relationship('AddressModel', lazy='select', foreign_keys=[address_id])


    @classmethod
    def __init__(self, emailaddress, first_name, last_name, user_type=UserType.viewer):
        """Inits UserModel with emailaddress, first_name, last_name, user_type."""
        super(UserModel, self).__init__()
        self.emailaddress = emailaddress
        self.first_name = first_name
        self.last_name = last_name
        self.user_type = user_type

    @property
    def user_type(self):
        """UserType: Defines the type of the User."""
        return UserType(self.user_type_id)

    @user_type.setter
    def user_type(self, new_user_type):
        if isinstance(new_user_type, UserType):
            self.user_type_id = new_user_type.value

basemodel.py

from datetime import datetime, date, time
from sqlalchemy.dialects import postgresql
from sqlalchemy.schema import ForeignKey
from sqlalchemy.ext.declarative import declared_attr, declarative_base

from core.database.dbmanager import DBManager

db = DBManager.get_db_instance()

class Base(object):
    """A model which functions as a basic model for other models.

    Adds the id property to other models.
    """

    id = db.Column(db.Integer, primary_key=True, nullable=False)


    def __init__(self):
        super(Base, self).__init__()


Base = declarative_base(cls=Base)

auditedmodel.py

from core.models.base.basemodel import db, postgresql, declared_attr, ForeignKey, datetime, Base

class Auditable(Base):
    """A model which functions as an auditable model for other models.

    Adds the created_by_id, created_by and creation_time properties to other models.
    """
    __abstract__ = True

    @declared_attr
    def created_by_id(cls):
        return db.Column(db.Integer, ForeignKey('Users.id'), nullable=False)

    @declared_attr
    def created_by(cls):
        return db.relationship('UserModel', lazy='select', foreign_keys=[cls.created_by_id])

    @declared_attr
    def creation_time(cls):
        return db.Column(db.DateTime, nullable=False, default=datetime.now())


    def __init__(self):
        super(Auditable, self).__init__()

auditeddeletemodel.py

from core.models.base.auditedmodel import db, postgresql, declared_attr, ForeignKey, Auditable

class AuditedDeletable(Auditable):
    """A model which functions as an auditeddeletable model for other models.

    Adds the is_deleted properties to other models.
    """
    __abstract__ = True

    @declared_attr
    def is_deleted(cls):
        return db.Column(db.Boolean, nullable=False, default=False)


    def __init__(self):
        super(AuditedDeletable, self).__init__()

1 个答案:

答案 0 :(得分:-1)

可能是由于与名为user_type的属性存在冲突的问题,而在你正在进行的构造函数中,“self.user_type = user_type”会覆盖此