Unverified Commit 35d21b68 authored by Serge S. Koval's avatar Serge S. Koval Committed by GitHub

Merge pull request #1981 from killthekitten/sqlalchemy-new-instance-hook

Add a hook for custom SQLAlchemy model initializers (#1927)
parents 714c42bf 94df8df4
...@@ -1111,6 +1111,20 @@ class ModelView(BaseModelView): ...@@ -1111,6 +1111,20 @@ class ModelView(BaseModelView):
return super(ModelView, self).handle_view_exception(exc) return super(ModelView, self).handle_view_exception(exc)
def build_new_instance(self):
"""
Build new instance of a model. Useful to override the Flask-Admin behavior
when the model has a custom __init__ method.
"""
model = self._manager.new_instance()
# TODO: We need a better way to create model instances and stay compatible with
# SQLAlchemy __init__() behavior
state = instance_state(model)
self._manager.dispatch.init(state, [], {})
return model
# Model handlers # Model handlers
def create_model(self, form): def create_model(self, form):
""" """
...@@ -1120,11 +1134,7 @@ class ModelView(BaseModelView): ...@@ -1120,11 +1134,7 @@ class ModelView(BaseModelView):
Form instance Form instance
""" """
try: try:
model = self._manager.new_instance() model = self.build_new_instance()
# TODO: We need a better way to create model instances and stay compatible with
# SQLAlchemy __init__() behavior
state = instance_state(model)
self._manager.dispatch.init(state, [], {})
form.populate_obj(model) form.populate_obj(model)
self.session.add(model) self.session.add(model)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment