Expose relation fields in the champ pickers and fix their column resolution

data_definition_options() used to exclude relation-type fields from its field list entirely, so a filter/binding could never reference "the linked object" of a row — and even when a relation field's clean name was typed manually (as the earlier "level.parcour" example needed), it silently matched nothing: every column lookup for a filter/repeater field used the field's own name, but a relation is actually stored in a "<field>_id" column (see create_definition.py), so the lookup always missed.

Relation fields now appear in the champ dropdowns (Répéteur's filtre_champ/filtre2_champ, Donnée liée's data_filtre_champ/data_filtre2_champ) labeled with the object they point to (e.g. "parcour (→ parcours)"), and a new _field_column() helper in filter_repeater_rows.py resolves the right "<field>_id" column whenever the field turns out to be a relation — used consistently by the filter comparison itself, the "{{Objet.champ}}" dynamic-value resolver, the repeater's row content ({{champ}}), and the Donnée liée row context. Jauge's own champ/champ_nom pickers (which need an actual displayable value, not an id) still exclude relations, both server- and client-side.

Verified end to end: the dropdown shows the relation field with its target-object label, and filtering "level" rows by the clean relation field name "parcour" (not "parcour_id") against a dynamic {{game.current_parcours}} reference now actually matches, alongside the existing "number" filter. Full suite green (89).
This commit is contained in:
william
2026-08-25 06:48:04 +02:00
parent 343732d7aa
commit aa3be503ce
6 changed files with 66 additions and 15 deletions
+15 -2
View File
@@ -10,6 +10,19 @@ import db
_REF_PATTERN = re.compile(r"^\{\{\s*([^.{}]+)\.([^.{}]+)\s*\}\}$")
def _field_column(definition, field_name):
"""Nom de la vraie colonne SQL d'un champ — un champ "relation" est
stocké dans une colonne "<champ>_id" (voir create_definition.py), jamais
sous son nom "propre" : sans ce cas particulier, filtrer ou comparer sur
un champ relation ne trouvait jamais la bonne colonne et ne matchait
donc jamais rien."""
col = db.slugify(field_name).replace("-", "_")
field = next((f for f in definition["fields"] if f["name"] == field_name), None)
if field and field["type"] == "relation":
col += "_id"
return col
def _resolve_filter_value(slug, raw_value):
"""Si raw_value est une référence "{{Objet.champ}}", va lire la valeur
ACTUELLE de ce champ sur la ligne la plus récente de cet objet (par
@@ -29,7 +42,7 @@ def _resolve_filter_value(slug, raw_value):
full = db.get_definition(slug, d["id"])
rows = db.list_rows(slug, full)
if rows:
return rows[0].get(db.slugify(field_name).replace("-", "_"))
return rows[0].get(_field_column(full, field_name))
return None
return None # objet de référence introuvable — filtre ne matchera rien plutôt que planter
@@ -81,7 +94,7 @@ def _apply_one_filter(slug, definition, rows, field_name, operator, raw_value):
return rows
expected = _resolve_filter_value(slug, raw_value or "")
field_type = next((f["type"] for f in definition["fields"] if f["name"] == field_name), "texte")
col = db.slugify(field_name).replace("-", "_")
col = _field_column(definition, field_name)
return [r for r in rows if _compare(r.get(col), operator, expected, field_type)]
+2 -2
View File
@@ -5,7 +5,7 @@ import db
from .style_string import _style_string
from .auto_id import _auto_id
from .apply_ctx import _apply_ctx
from .filter_repeater_rows import filter_repeater_rows
from .filter_repeater_rows import filter_repeater_rows, _field_column
from ..element_types.load_template_tree import _load_template_tree
@@ -32,7 +32,7 @@ def _render_repeater(el, meta, slug, children_map, ctx=None, parent_flex_directi
# qu'on sait que l'objet est valide.)
rows = db.list_rows(slug, definition)
rows = filter_repeater_rows(slug, definition, rows, attrs_raw)
field_cols = {f["name"]: db.slugify(f["name"]).replace("-", "_") for f in definition["fields"]}
field_cols = {f["name"]: _field_column(definition, f["name"]) for f in definition["fields"]}
# Modèle de ligne = un élément de jeu (conteneur stylisé réutilisable) :
# on recharge son arbre EN DIRECT à chaque ligne de données (jamais copié
+2 -2
View File
@@ -1,6 +1,6 @@
import db
from .filter_repeater_rows import filter_repeater_rows
from .filter_repeater_rows import filter_repeater_rows, _field_column
def _resolve_bound_row_ctx(slug, attrs):
@@ -35,4 +35,4 @@ def _resolve_bound_row_ctx(slug, attrs):
if not rows:
return None
row = rows[0]
return {f["name"]: str(row.get(db.slugify(f["name"]).replace("-", "_"), "") or "") for f in definition["fields"]}
return {f["name"]: str(row.get(_field_column(definition, f["name"]), "") or "") for f in definition["fields"]}