<\/script>')

Django error: __init __ () takes exactly 1 argument (2)

I wrote 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 wrote:

 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 runningerver 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) 
0
source share
1 answer

UPDATE:

You can always use the default constructor using the 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; there is no need to override it. Anyway, your code should work, unless something is overridden ...

0
source

Source: https://habr.com/ru/post/954554/


All Articles