Problem trying to add entry in db TypeError: __init__() error

I've searched for this and usually people have identation issues. I don't think the route should be in the class? But I've tried that and got the same error:

TypeError: __init__() takes exactly 1 argument (2 given)
class entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
url = db.Column(db.String(100), unique=True, nullable=False)

    def __repr__(self):
        return '<entry %r>' % self.url


@app.route('/new', methods = ['POST'])
def new():
   if request.method == 'POST':
      if not request.args.get('url'):
         flash('Please provide at least a url', 'error')
      else:
         entry2 = entry("hey") #error on this line, I put "hey" just to debug
         db.session.add(entry2)
         db.session.commit()

         flash('Record was successfully added')
         return redirect(url_for('show_all'))
   return render_template('index2.html')

Complete error:

127.0.0.1 - - [10/Feb/2019 13:27:14] "POST /new?url=http://google.com 

HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/dk9/code/prj/app2.py", line 41, in new
    entry2 = entry("hey")
TypeError: __init__() takes exactly 1 argument (2 given)

1 answer

  • answered 2019-02-10 12:19 Daniel Roseman

    You should not override the __init__ method in your entry model. Remove that method completely.

    Edit

    Models expect keyword arguments only. So you should do:

    entry2 = entry(url="hey")