-
Notifications
You must be signed in to change notification settings - Fork 200
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
Allow model columns to bear the same name as reserved wtforms.BaseForm attributes #658
Conversation
There was a problem hiding this 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
6c78992
to
4fb49fa
Compare
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
4fb49fa
to
5fcf7d8
Compare
I've added some tests as well to satisfy the codecov job. Feel free to refactor things around if you want to. |
There was a problem hiding this 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 👍
For example, a Model with a mapped column named
data
would collide with thewtforms.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 associatedField
object will:data_
name='data'
kwarg so that it is still displayed with thedata
label in the admin web UIInstead of directly passing
form.data
tomodel_view.insert_model
ormodel_view.update_model
, we first restore the original name (e.g.data_
todata
) so that the underlying SQLAlchemy can them work on the original column.Fixes #656