Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectFieldRenderer's that are on a relationship now use the default of the associated foreign key column #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions formalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
Binary file modified formalchemy/i18n_resources/de/LC_MESSAGES/formalchemy.mo
Binary file not shown.
Binary file modified formalchemy/i18n_resources/en/LC_MESSAGES/formalchemy.mo
Binary file not shown.
Binary file modified formalchemy/i18n_resources/es/LC_MESSAGES/formalchemy.mo
Binary file not shown.
Binary file modified formalchemy/i18n_resources/fr/LC_MESSAGES/formalchemy.mo
Binary file not shown.
Binary file modified formalchemy/i18n_resources/ja/LC_MESSAGES/formalchemy.mo
Binary file not shown.
Binary file modified formalchemy/i18n_resources/pl/LC_MESSAGES/formalchemy.mo
Binary file not shown.