1

Hi want to save the information being posted to the database. I am using python flask and this is the way i attempted it.

@app.route('/add-new-song',methods=['GET','POST'])
def add_new_song():
    form = NewSongForm(request.form)
    if form.validate_on_submit():
        new_song = SongBook()
        form.populate_obj(new_song)
        db.session.add(new_song)
        db.session.commit()
return render_template('add-new-song.html',form=form)

When i try this i get this error:
TypeError: __ init __() takes exactly 6 arguments (1 given)

Any thoughts on how to do this?

here are the models

class SongBook(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    song_book = db.Column(db.String(80))
    title = db.Column(db.String(120))
    artist = db.Column(db.String(120))
    disc_number = db.Column(db.String(120))
    track_number = db.Column(db.String(120))



    def __init__(self, song_book, title, artist, disc_number, track_number):
        self.song_book = song_book  
        self.title = title
        self.artist = artist
        self.disc_number = disc_number
        self.track_number = track_number

trace back

TypeError: __init__() takes exactly 6 arguments (1 given)

Traceback (most recent call last)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 271, in error_router
return original_handler(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/a/Desktop/ddtkaraoke.com/app.py", line 118, in add_new_song
new_song = SongBook()
TypeError: __init__() takes exactly 6 arguments (1 given)
David Gonzalez
  • 135
  • 3
  • 14
  • Perhaps you need to pass some arguments when you create the `SongBook`? If you can post the full traceback and possibly your `SongBook` class it might make things a little clearer. – Paul Rooney Sep 04 '16 at 23:52
  • Okay i posted the models – David Gonzalez Sep 04 '16 at 23:57
  • Thank you very much for your help. So there is no way of avoiding declaring all requests forms like so " title = request.from['title']". and just automatically pass the form fields? – David Gonzalez Sep 05 '16 at 00:04
  • http://stackoverflow.com/questions/6196622/using-wtforms-populate-obj-method-with-flask-micro-framework – iScrE4m Sep 05 '16 at 00:42
  • 1
    The problem isn't your form. It's the model. You need to make all of the arguments to `__init__` keyword arguments. Or, even better, since all you do is assign the values, you can remove yours and let the one provided by your base class do the work for you. – dirn Sep 05 '16 at 01:14
  • 1
    Removed the __int__ worked! thanks – David Gonzalez Sep 05 '16 at 02:35

0 Answers0