Skip to content

Initial data validation support on object create / update#84

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

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

Conversation

@radiophysicist
Copy link
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
Copy link

artefom commented Jan 22, 2022

Looks great!

@DoubleDi
Copy link

DoubleDi commented Feb 3, 2023

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
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)

Copy link
Contributor

@laggron42 laggron42 left a comment

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants