Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy Constructor

in the Flask-SQLAlchemy tutorial, a constructor for the User model is defined:

from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy  app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' db = SQLAlchemy(app)   class User(db.Model):     id = db.Column(db.Integer, primary_key=True)     username = db.Column(db.String(80), unique=True)     email = db.Column(db.String(120), unique=True)      def __init__(self, username, email):         self.username = username         self.email = email 

for a table with two columns, that might be acceptable, but what if I have tables with 10+ columns? do constructors have to be defined each time I define a new model?

like image 493
NightOwl Avatar asked Dec 08 '13 23:12

NightOwl


2 Answers

In most cases not defining a constructor in your model class gives you the correct behavior.

Flask-SQLAlchemy's base model class (which is also SQLAlchemy's declarative base class) defines a constructor that just takes **kwargs and stores all the arguments given, so it isn't really necessary to define a constructor.

If you do need to define a constructor to do some model specific initialization, then do so as follows:

class User(db.Model):     id = db.Column(db.Integer, primary_key=True)     username = db.Column(db.String(80), unique=True)     email = db.Column(db.String(120), unique=True)      def __init__(self, **kwargs):         super(User, self).__init__(**kwargs)         # do custom initialization here 

By letting the base class handle the **kwargs you free yourself from the complexity of initializing the fields of the model.

like image 54
Miguel Avatar answered Sep 28 '22 20:09

Miguel


I know this is a little old but regardless it should be useful for someone else with similar issue. If you encounter the "TypeError: init() takes exactly 1 argument (2 given)" - it means you need to provide the keywords when creating the objects to add to your database like so:

db.session.add(User(username='myname',email='my@email',password='mypassword')).

It is quite common to come across this small issue...but difficult to spot. I hope this helps.

like image 22
Helen Neely Avatar answered Sep 28 '22 19:09

Helen Neely