mirror of
https://github.com/fastapi-admin/fastapi-admin.git
synced 2025-08-16 03:40:26 +08:00
Merge pull request #91 from photopills/add-support-for-fk-fields
Improve `update` for fk fields
This commit is contained in:
@ -129,11 +129,21 @@ class Model(Resource):
|
|||||||
ret = []
|
ret = []
|
||||||
for field in cls.get_fields(is_display=False):
|
for field in cls.get_fields(is_display=False):
|
||||||
input_ = field.input
|
input_ = field.input
|
||||||
|
name = input_.context.get("name")
|
||||||
if isinstance(input_, inputs.DisplayOnly):
|
if isinstance(input_, inputs.DisplayOnly):
|
||||||
continue
|
continue
|
||||||
if isinstance(input_, inputs.File):
|
if isinstance(input_, inputs.File):
|
||||||
cls.enctype = "multipart/form-data"
|
cls.enctype = "multipart/form-data"
|
||||||
name = input_.context.get("name")
|
if (
|
||||||
|
isinstance(input_, inputs.ForeignKey)
|
||||||
|
and (obj is not None)
|
||||||
|
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)))
|
ret.append(await input_.render(request, getattr(obj, name, None)))
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
@ -159,6 +169,11 @@ class Model(Resource):
|
|||||||
if input_.context.get("disabled") or isinstance(input_, inputs.DisplayOnly):
|
if input_.context.get("disabled") or isinstance(input_, inputs.DisplayOnly):
|
||||||
continue
|
continue
|
||||||
name = input_.context.get("name")
|
name = input_.context.get("name")
|
||||||
|
if isinstance(input_, inputs.ForeignKey):
|
||||||
|
v = data.getlist(name)[0]
|
||||||
|
model = await input_.model.get(id=v)
|
||||||
|
ret[name] = model
|
||||||
|
continue
|
||||||
if isinstance(input_, inputs.ManyToMany):
|
if isinstance(input_, inputs.ManyToMany):
|
||||||
v = data.getlist(name)
|
v = data.getlist(name)
|
||||||
value = await input_.parse_value(request, v)
|
value = await input_.parse_value(request, v)
|
||||||
@ -289,6 +304,15 @@ class Model(Resource):
|
|||||||
ret.append(field)
|
ret.append(field)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_fk_field(cls):
|
||||||
|
ret = []
|
||||||
|
for field in cls.fields or cls.model._meta.fields:
|
||||||
|
if isinstance(field, Field):
|
||||||
|
field = field.name
|
||||||
|
if field in cls.model._meta.fk_fields:
|
||||||
|
ret.append(field)
|
||||||
|
return ret
|
||||||
|
|
||||||
class Dropdown(Resource):
|
class Dropdown(Resource):
|
||||||
resources: List[Type[Resource]]
|
resources: List[Type[Resource]]
|
||||||
|
@ -86,13 +86,14 @@ async def update(
|
|||||||
):
|
):
|
||||||
form = await request.form()
|
form = await request.form()
|
||||||
data, m2m_data = await model_resource.resolve_data(request, form)
|
data, m2m_data = await model_resource.resolve_data(request, form)
|
||||||
|
m2m_fields = model_resource.get_m2m_field()
|
||||||
async with in_transaction() as conn:
|
async with in_transaction() as conn:
|
||||||
obj = (
|
obj = (
|
||||||
await model.filter(pk=pk)
|
await model.filter(pk=pk)
|
||||||
.using_db(conn)
|
.using_db(conn)
|
||||||
.select_for_update()
|
.select_for_update()
|
||||||
.get()
|
.get()
|
||||||
.prefetch_related(*model_resource.get_m2m_field())
|
.prefetch_related(*m2m_fields)
|
||||||
)
|
)
|
||||||
await obj.update_from_dict(data).save(using_db=conn)
|
await obj.update_from_dict(data).save(using_db=conn)
|
||||||
for k, items in m2m_data.items():
|
for k, items in m2m_data.items():
|
||||||
@ -103,8 +104,9 @@ async def update(
|
|||||||
obj = (
|
obj = (
|
||||||
await model.filter(pk=pk)
|
await model.filter(pk=pk)
|
||||||
.using_db(conn)
|
.using_db(conn)
|
||||||
|
.select_related(*model_resource.get_fk_field())
|
||||||
.get()
|
.get()
|
||||||
.prefetch_related(*model_resource.get_m2m_field())
|
.prefetch_related(*m2m_fields)
|
||||||
)
|
)
|
||||||
inputs = await model_resource.get_inputs(request, obj)
|
inputs = await model_resource.get_inputs(request, obj)
|
||||||
if "save" in form.keys():
|
if "save" in form.keys():
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
<div class="form-label">{{ label }}</div>
|
<div class="form-label">{{ label }}</div>
|
||||||
<select class="form-select" name="{{ name }}" id="{{ id }}">
|
<select class="form-select" name="{{ name }}" id="{{ id }}">
|
||||||
{% for option in options %}
|
{% for option in options %}
|
||||||
<option value="{{ option[1] }}" {% if option[1] == value %}
|
<option value="{{ option[1] }}" {% if option[0] == value %}
|
||||||
selected
|
selected
|
||||||
{% endif %} >{{ option[0] }}</option>
|
{% endif %} >{{ option[0] }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
Reference in New Issue
Block a user