Show db value as html selected option

This commit is contained in:
Diogo
2021-10-19 13:18:10 +01:00
parent 0170abb5c3
commit 50df1bb2f6
2 changed files with 8 additions and 2 deletions

View File

@@ -129,11 +129,17 @@ class Model(Resource):
ret = []
for field in cls.get_fields(is_display=False):
input_ = field.input
name = input_.context.get("name")
if isinstance(input_, inputs.DisplayOnly):
continue
if isinstance(input_, inputs.File):
cls.enctype = "multipart/form-data"
name = input_.context.get("name")
if isinstance(input_, inputs.ForeignKey) and name in obj._meta.fk_fields:
await obj.fetch_related(name)
# Value must be the string representation of the fk obj
value = str(getattr(obj, name, None))
ret.append(await input_.render(request, value))
continue
ret.append(await input_.render(request, getattr(obj, name, None)))
return ret

View File

@@ -3,7 +3,7 @@
<div class="form-label">{{ label }}</div>
<select class="form-select" name="{{ name }}" id="{{ id }}">
{% for option in options %}
<option value="{{ option[1] }}" {% if option[1] == value %}
<option value="{{ option[1] }}" {% if option[0] == value %}
selected
{% endif %} >{{ option[0] }}</option>
{% endfor %}