mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2026-03-13 10:32:25 +08:00
Show db value as html selected option
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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 %}
|
||||
|
||||
Reference in New Issue
Block a user