Changes between Version 3 and Version 10 of Ticket #25227
- Timestamp:
- Aug 6, 2015, 6:29:19 AM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #25227 – Description
v3 v10 1 Add utility method `get_updated_model()` to `ModelForm` and, additionally, add utility method `get_updated_models()` to `FormSet`. 1 Add utility method get_updated_model() to ModelForm 2 3 Additionally, add utility method get_updated_models() to FormSet 2 4 3 5 Rationale: 4 6 5 While doing the djangogirls tutorial, I noticed that `ModelForm.save(commit=False)`is given to newcomers as a reasonable way to acquire a form's populated model. This is an antipattern for several reasons:7 While doing the djangogirls tutorial, I noticed that ModelForm.save(commit=False) is given to newcomers as a reasonable way to acquire a form's populated model. This is an antipattern for several reasons: 6 8 7 9 - It's counterintuitive. To a newcomer, it's the same as ``save(save=no)``. 8 10 - It's a leaky abstraction. Understanding it requires understanding that the save method does two things: a) prepare the model, and b) save it to the database. 9 - It doesn't name its effect or return well. It returns a model, not a form.11 - It doesn't name its effect or return well. 10 12 11 13 All these problems are addressed in the current patch. Changes: 12 14 13 - Implement `ModelForm.get_updated_model()`14 - Implement `FormSet.get_updated_models()`15 - Refactor `models.py` minimally for the above to work.16 - Tests not added for `get_updated_model()` nor `get_updated_models()` because the changes are small enough to be considered as a refactoring.17 - Both the tests and `contrib.auth` have been modified: any call to `save(commit=False)` for the purpose of obtaining a populated model has been replaced by `get_updated_model()`. Tests still pass, I'm confident it's a successful refactor.18 - `docs/customizing.txt` has also been modified to showcase the new method.15 - Implement ModelForm.get_updated_model() 16 - Implement FormSet.get_updated_models() 17 - Refactor ModelForm.save() and FormSet.save() to allow the above. 18 - Both the tests and contrib.auth have been modified: any call to save(commit=False) for the purpose of obtaining a populated model has been replaced by get_updated_model(). Tests still pass, I'm confident it's a successful refactor. 19 - New tests have been added for the new methods in different contexts (from a ModelForm, from a FormSet, etc). 20 - documentation has also been modified to showcase the new methods. 19 21 20 22 Notes: 21 23 22 Uses of `ModelForm.save(commit=False)`in the codebase have been left alone wherever it was used for its side effects and not for its return value.24 Uses of ModelForm.save(commit=False) in the codebase have been left alone wherever it was used for its side effects and not for its return value. 23 25 24 The PR at https://github.com/DjangoGirls/tutorial/pull/450 depends on the present patch. Its changeset there explains the rationale for this addition better than anything else. 26 The Djangogirls tutorial has a PR that depends on the changes in the present one: 27 28 https://github.com/DjangoGirls/tutorial/pull/450