From c561980bef0ff9427dca6dbb5e768eea94b6c8b3 Mon Sep 17 00:00:00 2001 From: James Curtin Date: Sat, 8 Jun 2019 12:10:33 -0400 Subject: [PATCH 1/2] Support passing custom sets of kwargs when creating a reference column --- .../{{cookiecutter.app_name}}/database.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py index 3a8883d..10d9ce2 100644 --- a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py +++ b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py @@ -62,7 +62,7 @@ class SurrogatePK(object): return None -def reference_col(tablename, nullable=False, pk_name='id', **kwargs): +def reference_col(tablename, nullable=False, pk_name='id', foreign_key_kwargs=None, column_kwargs=None): """Column that adds primary key foreign key reference. Usage: :: @@ -70,6 +70,11 @@ def reference_col(tablename, nullable=False, pk_name='id', **kwargs): category_id = reference_col('category') category = relationship('Category', backref='categories') """ + if foreign_key_kwargs is None: + foreign_key_kwargs = {} + if column_kwargs is None: + column_kwargs = {} + return Column( - db.ForeignKey('{0}.{1}'.format(tablename, pk_name)), - nullable=nullable, **kwargs) + db.ForeignKey('{0}.{1}'.format(tablename, pk_name), **foreign_key_kwargs), + nullable=nullable, **column_kwargs) From 90ed8fec39c9507c4abb3ec31d55def738647a40 Mon Sep 17 00:00:00 2001 From: James Curtin Date: Sun, 9 Jun 2019 21:58:24 -0400 Subject: [PATCH 2/2] Simplify setting kwarg defaults when value is not passed --- .../{{cookiecutter.app_name}}/database.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py index 10d9ce2..e183940 100644 --- a/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py +++ b/{{cookiecutter.app_name}}/{{cookiecutter.app_name}}/database.py @@ -70,10 +70,8 @@ def reference_col(tablename, nullable=False, pk_name='id', foreign_key_kwargs=No category_id = reference_col('category') category = relationship('Category', backref='categories') """ - if foreign_key_kwargs is None: - foreign_key_kwargs = {} - if column_kwargs is None: - column_kwargs = {} + foreign_key_kwargs = foreign_key_kwargs or {} + column_kwargs = column_kwargs or {} return Column( db.ForeignKey('{0}.{1}'.format(tablename, pk_name), **foreign_key_kwargs),