Skip to content

Initial data validation support on object create / update - #84

Open
radiophysicist wants to merge 3 commits into
fastapi-admin:devfrom
radiophysicist:validation-support
Open

Initial data validation support on object create / update#84
radiophysicist wants to merge 3 commits into
fastapi-admin:devfrom
radiophysicist:validation-support

Conversation

@radiophysicist

Copy link
Copy Markdown
Contributor
  • In recourse model class method for data validation is extracted
  • More error handling added in create / update request handlers
  • Error message rendering added into create / update page templates
@radiophysicist
radiophysicist force-pushed the validation-support branch 2 times, most recently from b4cf8e1 to 0cda518 Compare September 20, 2021 16:12
@artefom

artefom commented Jan 22, 2022

Copy link
Copy Markdown

Looks great!

@DoubleDi

DoubleDi commented Feb 3, 2023

Copy link
Copy Markdown

Is there a possibility we can merge this?

Comment on lines +93 to +113
if not error:
async with in_transaction() as conn:
obj = (
await model.filter(pk=pk)
.using_db(conn)
.select_for_update()
.get()
.prefetch_related(*model_resource.get_m2m_field())
)
await obj.update_from_dict(data).save(using_db=conn)
for k, items in m2m_data.items():
m2m_obj = getattr(obj, k)
await m2m_obj.clear()
if items:
await m2m_obj.add(*items)
obj = (
await model.filter(pk=pk)
.using_db(conn)
.get()
.prefetch_related(*model_resource.get_m2m_field())
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this on my end to also pass Tortoise errors on edition and not just on creation

Suggested change
if not error:
async with in_transaction() as conn:
obj = (
await model.filter(pk=pk)
.using_db(conn)
.select_for_update()
.get()
.prefetch_related(*model_resource.get_m2m_field())
)
await obj.update_from_dict(data).save(using_db=conn)
for k, items in m2m_data.items():
m2m_obj = getattr(obj, k)
await m2m_obj.clear()
if items:
await m2m_obj.add(*items)
obj = (
await model.filter(pk=pk)
.using_db(conn)
.get()
.prefetch_related(*model_resource.get_m2m_field())
)
if not error:
try:
async with in_transaction() as conn:
obj = (
await model.filter(pk=pk)
.using_db(conn)
.select_for_update()
.get()
.prefetch_related(*model_resource.get_m2m_field())
)
await obj.update_from_dict(data).save(using_db=conn)
for k, items in m2m_data.items():
m2m_obj = getattr(obj, k)
await m2m_obj.clear()
if items:
await m2m_obj.add(*items)
obj = (
await model.filter(pk=pk)
.using_db(conn)
.get()
.prefetch_related(*model_resource.get_m2m_field())
)
except BaseORMException as exc:
error = str(exc)

@laggron42 laggron42 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I tested your PR and, after solving a few conflicts, turns out great!

I copied logic from create over update to also pass validation errors to object editions.

Edit: @long2ice I have rebased and solved conflicts on this branch if you're interested in this PR, currently running in prod just fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants