Django的错误:__init __()恰恰1参数(2给出) [英] django error: __init__() takes exactly 1 argument (2 given)
问题描述
我已经写了名为图书馆一SQLAlchemy的模型:
类库(基本):
__tablename__ ='库'
ID =列(整数,primary_key =真)
细节=列(字符串) 高清__init __(自我,详细说明):
self.details =细节 高清__repr __(个体经营):
返回U图书馆(%S)%(self.details)
然后,在views.py文件中,我已经写了:
DEF is_lib_empty():
返回LEN(session.query(库)。所有())≤= 0高清populateLib():
new_libs = [库(一),库(两节),图书馆('三'),图书馆('四')]
session.add_all(new_libs)
session.commit()高清指数(要求):
如果is_lib_empty():
populateLib() 库= session.query(库)。所有()
返回render_to_response('../模板/ index.html的',{'库':库})
然后我跑的蟒蛇manage.py的runserver 的,它显示了我的错误消息:
__ __的init()恰恰1参数(2给出)
我应该怎么做来解决这个问题?
类型错误的/
__init __()恰恰1参数(2给出)
请求方法:GET
Django的版本:1.3.1
异常类型:类型错误
例外的值:
__init __()恰恰1参数(2给出)
例外地点:/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py在populateLib 25行
Python的可执行文件:/sw/bin/python2.7
Python版本:2.7.2
Python的路径:
['/ CS / wetlab /利莫尔/工作区/ 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']
服务器时间:孙老师,2012年7月1日5时50分03秒-0500
环境:
请求方法:GETDjango的版本:1.3.1
Python版本:2.7.2
安装的应用程序:
['loose_coupling.with_sqlalchemy']
安装中间件:
(django.middleware.common.CommonMiddleware',)
追溯:
文件/sw/lib/python2.7/site-packages/django/core/handlers/base.py在get_response
111.响应=回调(请求,* callback_args,** callback_kwargs)
文件/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py,在指数
39. populateLib()
文件/cs/wetlab/Limor/workspace/Yeasti/Yeasti/loose_coupling/../loose_coupling/with_sqlalchemy/views.py在populateLib
25. new_libs = [库(一),库(两节),图书馆('三'),图书馆('四')]异常类型:类型错误的/
异常值:__init __()恰恰1参数(2给出)
更新:
您可以随时使用默认的构造函数,使用关键字参数:
库(细节='一些细节')
在 http://docs.sqlalchemy.org/en/rel_0_7描述/orm/tutorial.html
这是默认的构造函数,不需要重写它的支持。无论如何,你的code应该工作,除非有一些地方覆盖...
I've written a sqlalchemy model called 'library':
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)
Then, inside the views.py file, I've written:
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})
And then I run python manage.py runserver, and it shows me an error message:
__init__() takes exactly 1 argument (2 given)
What should I do to fix this?
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)
UPDATE:
You can always use the default constructor, using keyword arguments:
Library(details='some details')
as described in http://docs.sqlalchemy.org/en/rel_0_7/orm/tutorial.html
This is supported by the default constructor, no need to override it. Anyway, your code should work, unless there is some override somewhere...
这篇关于Django的错误:__init __()恰恰1参数(2给出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!