Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

one-to-one inline forms

  • Loading branch information...
commit 12c509f8f8189a4f8ddfd3164d9f538aea7b2623 1 parent e849aee
@baverman authored
View
22 flask_admin/contrib/sqlamodel/fields.py
@@ -8,7 +8,7 @@
from wtforms.validators import ValidationError
from .tools import get_primary_key
-from flask.ext.admin.model.fields import InlineFieldList, InlineModelFormField
+from flask.ext.admin.model.fields import InlineFieldList, InlineModelFormField, FormField, InlineFormWidget
try:
@@ -239,3 +239,23 @@ def get_pk_from_identity(obj):
# TODO: Remove me
cls, key = identity_key(instance=obj)
return u':'.join(unicode(x) for x in key)
+
+
+class InlineOneToOneFormField(FormField):
+ widget = InlineFormWidget()
+
+ def __init__(self, form, model, **kwargs):
+ self.model = model
+ super(InlineOneToOneFormField, self).__init__(form, **kwargs)
+
+ def get_pk(self):
+ return getattr(self.form, self._pk).data
+
+ def populate_obj(self, obj, parent_name):
+ child_obj = getattr(obj, parent_name)
+ if child_obj is None:
+ child_obj = self.model()
+ setattr(obj, parent_name, child_obj)
+
+ for name, field in self.form._fields.iteritems():
+ field.populate_obj(child_obj, name)
View
23 flask_admin/contrib/sqlamodel/form.py
@@ -8,7 +8,8 @@
InlineFormAdmin, InlineModelConverterBase)
from .validators import Unique
-from .fields import QuerySelectField, QuerySelectMultipleField, InlineModelFormList
+from .fields import (QuerySelectField, QuerySelectMultipleField, InlineModelFormList,
+ InlineOneToOneFormField)
try:
# Field has better input parsing capabilities.
@@ -485,13 +486,17 @@ def contribute(self, converter, model, form_class, inline_model):
if label:
kwargs['label'] = label
- # Contribute field
- setattr(form_class,
- forward_prop.key,
- InlineModelFormList(child_form,
- self.session,
- info.model,
- reverse_prop.key,
- **kwargs))
+ if forward_prop.uselist:
+ # Contribute field
+ setattr(form_class,
+ forward_prop.key,
+ InlineModelFormList(child_form,
+ self.session,
+ info.model,
+ reverse_prop.key,
+ **kwargs))
+ else:
+ setattr(form_class, forward_prop.key, InlineOneToOneFormField(child_form,
+ info.model, **kwargs))
return form_class
Please sign in to comment.
Something went wrong with that request. Please try again.