diff --git a/formalchemy/fields.py b/formalchemy/fields.py index eb3e684..f9c3c1c 100644 --- a/formalchemy/fields.py +++ b/formalchemy/fields.py @@ -15,7 +15,7 @@ from sqlalchemy.orm.interfaces import ONETOMANY from sqlalchemy.orm import class_mapper, Query from sqlalchemy.orm.attributes import ScalarAttributeImpl, ScalarObjectAttributeImpl, CollectionAttributeImpl -from sqlalchemy.orm.properties import CompositeProperty, ColumnProperty +from sqlalchemy.orm.properties import CompositeProperty, ColumnProperty, RelationshipProperty from sqlalchemy import exceptions as sqlalchemy_exceptions from sqlalchemy.orm import object_session from formalchemy import helpers as h @@ -174,8 +174,17 @@ def value(self): return v value = self.field.model_value - if value is None: + + if value is None and _pk(self.field.parent.model) is None: + if (hasattr(self.field, '_property') and + isinstance(self.field._property, RelationshipProperty)): + try: + value = self.field._property.local_side[0].default.arg + except Exception: + pass + elif value is None: return None + if self.field.is_collection: return [self.stringify_value(v) for v in value] else: @@ -830,6 +839,7 @@ def render(self, options, **kwargs): L = [(k, self.stringify_value(v)) for k, v in L] else: L = [_stringify(k) for k in L] + return h.select(self.name, self.value, L, **kwargs) def render_readonly(self, options=None, **kwargs): diff --git a/formalchemy/i18n_resources/de/LC_MESSAGES/formalchemy.mo b/formalchemy/i18n_resources/de/LC_MESSAGES/formalchemy.mo index f687095..7e55a04 100644 Binary files a/formalchemy/i18n_resources/de/LC_MESSAGES/formalchemy.mo and b/formalchemy/i18n_resources/de/LC_MESSAGES/formalchemy.mo differ diff --git a/formalchemy/i18n_resources/en/LC_MESSAGES/formalchemy.mo b/formalchemy/i18n_resources/en/LC_MESSAGES/formalchemy.mo index 7ce1dbc..cd2ec14 100644 Binary files a/formalchemy/i18n_resources/en/LC_MESSAGES/formalchemy.mo and b/formalchemy/i18n_resources/en/LC_MESSAGES/formalchemy.mo differ diff --git a/formalchemy/i18n_resources/es/LC_MESSAGES/formalchemy.mo b/formalchemy/i18n_resources/es/LC_MESSAGES/formalchemy.mo index a832eaf..c74b2a3 100644 Binary files a/formalchemy/i18n_resources/es/LC_MESSAGES/formalchemy.mo and b/formalchemy/i18n_resources/es/LC_MESSAGES/formalchemy.mo differ diff --git a/formalchemy/i18n_resources/fr/LC_MESSAGES/formalchemy.mo b/formalchemy/i18n_resources/fr/LC_MESSAGES/formalchemy.mo index 54b7991..73ac18f 100644 Binary files a/formalchemy/i18n_resources/fr/LC_MESSAGES/formalchemy.mo and b/formalchemy/i18n_resources/fr/LC_MESSAGES/formalchemy.mo differ diff --git a/formalchemy/i18n_resources/ja/LC_MESSAGES/formalchemy.mo b/formalchemy/i18n_resources/ja/LC_MESSAGES/formalchemy.mo index ed93ba2..a57a67c 100644 Binary files a/formalchemy/i18n_resources/ja/LC_MESSAGES/formalchemy.mo and b/formalchemy/i18n_resources/ja/LC_MESSAGES/formalchemy.mo differ diff --git a/formalchemy/i18n_resources/pl/LC_MESSAGES/formalchemy.mo b/formalchemy/i18n_resources/pl/LC_MESSAGES/formalchemy.mo index 324b7fc..bd34e2b 100644 Binary files a/formalchemy/i18n_resources/pl/LC_MESSAGES/formalchemy.mo and b/formalchemy/i18n_resources/pl/LC_MESSAGES/formalchemy.mo differ