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

Implement "frozen" fields #2116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

falko17
Copy link
Contributor

@falko17 falko17 commented Jun 3, 2021

This adds support for frozen fields: By setting a model view's freeze_fields member to True (default is False), clicking on "Save and add another" will cause the fields' contents to stay the same instead of being emptied.

This can be useful to quickly enter similar data successively.
For example, assume that there is a model for users where each user is represented by a name, and has certain roles and an expiry date. Now let's say the administrator needs to create entries for 10 new users with the same set of roles and the same expiry date. With this feature, instead of having to enter the roles and expiry date again for each new user (10 times in total), the administrator only has to do it once — the fields will stay "frozen", and after clicking on "Save and add another", only the name of each new user has to be modified.

This adds to new fields to the BaseModelView: `allow_frozen_fields` and
`excluded_frozen_fields`. Setting `allow_frozen_fields` to True adds
another button to the create and the edit view which acts like
"Save and Add Another", but "freezes" the fields (i.e.,
doesn't reset their contents).

`excluded_frozen_fields` can be used to specify fields
which should always be cleared (e.g., password fields).
@falko17
Copy link
Contributor Author

falko17 commented Oct 15, 2021

I agree, having the two members allow_frozen_fields and exclude_frozen_fields does seem like a better idea. I'll try to get around to implementing this proposed change soon.

@caffeinatedMike
Copy link
Contributor

Sounds good! Also, please remember to update the corresponding documentation :)

Copy link
Contributor

@caffeinatedMike caffeinatedMike left a comment

Choose a reason for hiding this comment

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

I like the idea of this, but I think this behavior should be "opt-in" to enforce best-practices in data cleanliness.

Reasoning:
We might not want a User model to have freeze_fields set to True by default because it would encourage creating many users with the same password.

Proposed Adjustment:
Instead of freeze_fields, what about adding two properties? allow_frozen_fields and exclude_frozen_fields

If allow_frozen_fields = True, then a checkbox is rendered somewhere in the add/edit form (maybe at the bottom near the submission button or at top, open to ideas) that allows the user to enable this functionality (we'd need to handle preserving the checkbox value between form submissions/Create and Add Another). In addition to that, exclude_frozen_fields can include a list of fields that should not be persisted across form submissions (such as a password field).

@falko17
Copy link
Contributor Author

falko17 commented Aug 9, 2022

Alright, it took me some time to get around to it, but I've implemented the suggested change now. The only difference is that instead of a checkbox (which would only have affected a single button anyway) I've simply added another button if the user wishes to enable the "freezing" behavior.

@falko17
Copy link
Contributor Author

falko17 commented Sep 22, 2024

I've updated the pull request to work with the latest master now, but tests on Python 3.8, 3.9, and 3.12 still fail (other Python versions seem fine). To be honest, I'm not sure what causes this. I don't really see the relation between the error message and my changes yet.

I also haven't really looked at this in 2 years, so I hope the PR itself is still fine—please tell me if I should change anything (apart from fixing the tests) 😅

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

Successfully merging this pull request may close these issues.

2 participants