首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django错误:__init__()只接受一个参数(2个给定)

django错误:__init__()只接受一个参数(2个给定)
EN

Stack Overflow用户
提问于 2012-07-01 10:40:06
回答 1查看 5.1K关注 0票数 0

我写了一个叫做“库”的sqlalchemy模型:

代码语言:javascript
复制
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文件中,我编写了:

代码语言:javascript
复制
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})  

然后运行,它向我显示一个错误消息:

代码语言:javascript
复制
__init__() takes exactly 1 argument (2 given)

我该怎么做才能解决这个问题?

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2012-07-01 11:00:26

更新:

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

代码语言:javascript
复制
Library(details='some details')

7/orm/tutorial.html所述

默认构造函数支持这一点,不需要重写它。不管怎么说,你的代码应该能工作,除非有什么覆盖.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11281279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档