Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow model columns to bear the same name as reserved wtforms.BaseForm attributes #658

Merged
merged 5 commits into from
Nov 8, 2023

Conversation

brouberol
Copy link
Contributor

@brouberol brouberol commented Oct 27, 2023

For example, a Model with a mapped column named data would collide with the wtforms.Form.data property, which would generate errors when processing or rendering the form, as reported in #656.

What we do here is silently rename the column when entering and leaving WTForms territory. Taking the example of a column named data, the associated Field object will:

  • be listed under the form key data_
  • be passed a name='data' kwarg so that it is still displayed with the data label in the admin web UI

Instead of directly passing form.data to model_view.insert_model or model_view.update_model, we first restore the original name (e.g. data_ to data) so that the underlying SQLAlchemy can them work on the original column.

Note: this is a bit crude and I think the naming could be improved. Feel free to refactor at will.

Fixes #656

Copy link
Owner

@aminalaee aminalaee left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, I think it looks good, just some cleanup is needed

sqladmin/forms.py Outdated Show resolved Hide resolved
sqladmin/application.py Outdated Show resolved Hide resolved
@brouberol brouberol force-pushed the wtforms-reserved-attributes branch 4 times, most recently from 6c78992 to 4fb49fa Compare October 27, 2023 12:31
For example, a Model with a mapped column named `data` would collide with the
[`wtforms.Form.data`](https://github.com/aminalaee/sqladmin/blob/main/sqladmin/forms.py#L619-L634)
property, which would generate errors when processing or rendering the form.

What we do here is silently rename the column when entering and leaving WTForms territory.
Taking the example of a column named `data`, the associated `Field` object will:
- be listed under the form key `data_`
- be passed a `name='data'` kwarg so that it is still displayed with the `data` label in the admin web UI

Instead of directly passing `form.data` to `model_view.insert_model` or `model_view.update_model`, we first restore the original name (e.g. `data_` to `data`) so that the underlying SQLAlchemy can them work on the originalcolumn.

> **Note**: this is a bit crude and I think the naming could be improved. Feel free to refactor at will.

Fixes aminalaee#656
@brouberol brouberol force-pushed the wtforms-reserved-attributes branch from 4fb49fa to 5fcf7d8 Compare October 27, 2023 12:32
@brouberol
Copy link
Contributor Author

I've added some tests as well to satisfy the codecov job. Feel free to refactor things around if you want to.

@brouberol brouberol changed the title Allow model columns to bear the same name as reserved WTForms attributes Allow model columns to bear the same name as reserved wtforms.BaseForm attributes Oct 27, 2023
Copy link
Owner

@aminalaee aminalaee left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution 👍

@aminalaee aminalaee merged commit 7ca4d02 into aminalaee:main Nov 8, 2023
6 checks passed
@aminalaee aminalaee mentioned this pull request Nov 14, 2023
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.

Can't save with a ModelView to a Model that has a column named data
2 participants