SQLAlchemy sets default to nullable = False

I use SQLAlchemy for Flask to create some models. The problem is that almost all of my columns require nullable=False , so I'm looking for a way to set this option by default when creating a column. Of course, I could add them manually (as a Vim exercise), but today I don't like it. For reference, here is what my setup ( models.py ) looks like:

 from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), nullable=False) 

and much more. Is there an easy way to do this?

Thanks in advance.

+7
python flask-sqlalchemy sqlalchemy
source share
1 answer

just create a wrapper that sets it

 def NullColumn(*args,**kwargs): kwargs["nullable"] = kwargs.get("nullable",True) return db.Column(*args,**kwargs) ... username = NullColumn(db.String(80)) 

using functools.partial as recommended in the comments

 from functools import partial NullColumn = partial(Column,nullable=True) 
+15
source share

All Articles