Various bug fixes. Still trying to fix cartesian issue on search.
This commit is contained in:
parent
0dbf246bdb
commit
3c07741500
9 changed files with 412 additions and 94 deletions
|
|
@ -413,7 +413,7 @@ def _value_label_for_field(field: dict, mapper, values_map: dict, instance, sess
|
|||
if not rel_prop:
|
||||
return None
|
||||
|
||||
rid = _coerce_fk_value(values_map, instance, base)
|
||||
rid = _coerce_fk_value(values_map, instance, base, rel_prop)
|
||||
rel_obj = _resolve_rel_obj(values_map, instance, base)
|
||||
|
||||
label_spec = (
|
||||
|
|
@ -493,7 +493,7 @@ class _SafeObj:
|
|||
val = _get_loaded_attr(self._obj, name)
|
||||
return "" if val is None else _SafeObj(val)
|
||||
|
||||
def _coerce_fk_value(values: dict | None, instance: Any, base: str):
|
||||
def _coerce_fk_value(values: dict | None, instance: Any, base: str, rel_prop: Optional[RelationshipProperty] = None):
|
||||
"""
|
||||
Resolve current selection for relationship 'base':
|
||||
1) values['<base>_id']
|
||||
|
|
@ -540,6 +540,25 @@ def _coerce_fk_value(values: dict | None, instance: Any, base: str):
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
# Fallback: if we know the relationship, try its local FK column names
|
||||
if rel_prop is not None:
|
||||
try:
|
||||
st = inspect(instance) if instance is not None else None
|
||||
except Exception:
|
||||
st = None
|
||||
|
||||
# Try values[...] first
|
||||
for col in getattr(rel_prop, "local_columns", []) or []:
|
||||
key = getattr(col, "key", None) or getattr(col, "name", None)
|
||||
if not key:
|
||||
continue
|
||||
if isinstance(values, dict) and key in values and values[key] not in (None, ""):
|
||||
return values[key]
|
||||
if set is not None:
|
||||
attr = st.attrs.get(key) if hasattr(st, "attrs") else None
|
||||
if attr is not None and attr.loaded_value is not NO_VALUE:
|
||||
return attr.loaded_value
|
||||
|
||||
return None
|
||||
|
||||
def _is_many_to_one(mapper, name: str) -> Optional[RelationshipProperty]:
|
||||
|
|
@ -1136,7 +1155,7 @@ def render_form(
|
|||
base = name[:-3]
|
||||
rel_prop = mapper.relationships.get(base)
|
||||
if isinstance(rel_prop, RelationshipProperty) and rel_prop.direction.name == "MANYTOONE":
|
||||
values_map[name] = _coerce_fk_value(values, instance, base)
|
||||
values_map[name] = _coerce_fk_value(values, instance, base, rel_prop) # add rel_prop
|
||||
|
||||
else:
|
||||
# Auto-generate path (your original behavior)
|
||||
|
|
@ -1169,7 +1188,7 @@ def render_form(
|
|||
fk_fields.add(f"{base}_id")
|
||||
|
||||
# NEW: set the current selection for this dropdown
|
||||
values_map[f"{base}_id"] = _coerce_fk_value(values, instance, base)
|
||||
values_map[f"{base}_id"] = _coerce_fk_value(values, instance, base, prop)
|
||||
|
||||
# Then plain columns
|
||||
for col in model_cls.__table__.columns:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue