django错误:uuu init_uuu()正好接受1个参数(给定2个)

django错误:uuu init_uuu()正好接受1个参数(给定2个),django,sqlalchemy,arguments,Django,Sqlalchemy,Arguments,我已经编写了一个名为“库”的sqlalchemy模型: class Library(Base): __tablename__ = 'library' id = Column(Integer, primary_key=True) details = Column(String) def __init__(self, details): self.details = details def __repr__(self): r

我已经编写了一个名为“库”的sqlalchemy模型:

class Library(Base):
    __tablename__ = 'library'
    id = Column(Integer, primary_key=True)
    details = Column(String)

    def __init__(self, details):
        self.details = details

    def __repr__(self):
        return u"Library(%s)" % (self.details)  
然后,在views.py文件中,我编写了:

def is_lib_empty():
    return len(session.query(Library).all()) <= 0

def populateLib():
    new_libs = [Library('one'), Library('two'), Library('three'), Library('four')]
    session.add_all(new_libs)
    session.commit()

def index(request):
    if is_lib_empty():    
        populateLib()

    libs = session.query(Library).all()
    return render_to_response('../templates/index.html',{'libs':libs})  
我该怎么做才能解决这个问题

TypeError at /
__init__() takes exactly 1 argument (2 given)
Request Method: GET
Django Version: 1.3.1
Exception Type: TypeError
Exception Value:    
__init__() takes exactly 1 argument (2 given)
Exception Location: /cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py in populateLib, line 25
Python Executable:  /sw/bin/python2.7
Python Version: 2.7.2
Python Path:    
['/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling',
 '/sw/lib/python27.zip',
 '/sw/lib/python2.7',
 '/sw/lib/python2.7/plat-darwin',
 '/sw/lib/python2.7/plat-mac',
 '/sw/lib/python2.7/plat-mac/lib-scriptpackages',
 '/sw/lib/python2.7/lib-tk',
 '/sw/lib/python2.7/lib-old',
 '/sw/lib/python2.7/lib-dynload',
 '/sw/lib/python2.7/site-packages',
 '/sw/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time:    Sun, 1 Jul 2012 05:50:03 -0500  


Environment:
Request Method: GET

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['loose_coupling.with_sqlalchemy']
Installed Middleware:
('django.middleware.common.CommonMiddleware',)


Traceback:
File "/sw/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py" in index
  39.     populateLib()
File "/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py" in populateLib
  25.     new_libs = [Library('one'), Library('two'), Library('three'), Library('four')]

Exception Type: TypeError at /
Exception Value: __init__() takes exactly 1 argument (2 given)
更新:

始终可以使用默认构造函数,使用关键字参数:

Library(details='some details')
如中所述


默认构造函数支持这一点,无需重写它。无论如何,您的代码应该可以工作,除非在某个地方有一些重写…

请发布异常的完整回溯,以便我们可以看到错误的来源!TypeError at/\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,第25行Python可执行文件:/sw/bin/python2.7 Python版本:2.7.2Python路径:['/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling'、'/sw/lib/python2.7'、'/sw/lib/python2.7/plat darwin'、'/sw/lib/python2.7/plat mac'、'/sw/lib/python2.7/plat mac/lib脚本包'、'/sw/lib/python2.7/lib/lib-tk'、'/sw/lib/python2.7/lib/python2.7/lib/python2.7/python2.7/lib/python2.7/python2.7/python2.7'、'/python2.7/spackpackamposite','/sw/lib/python2.7/site packages/setuptools-0.6c11-py2.7.egg info']@limlim:把它放到问题中,这样我们就可以看到换行符了!@limlim:我怀疑您正在使用import*中的
导入相当多的模块,并且您正在使用的
类不是您认为的类,而是来自其他一些模块。您能否验证代码行
new\u libs=[…]
实际上是您的类(例如,通过在上面放置一个
打印(库)
,应该读作
)?我在这里复制代码时犯了一个错误。正确的行是:new_libs=[Library('one')、Library('two')、Library('three')、Library('three')、Library('four')],但它仍然不起作用。kay,错误表示init需要1个参数(self)。从您的代码中可以看出,它也需要“详细信息”。这是库模型的真实代码吗?我已经更新了答案。不需要重写init方法,它支持将列值作为关键字参数传递。但无论如何,您的代码似乎是有效的。不知道出了什么问题,至少在教程中有一个类似的示例。。
Library(details='some details')