Opened 2 hours ago
Last modified 15 minutes ago
#35964 assigned Cleanup/optimization
Remove unnecessary calls and fix dictionary key order in Formset documentation examples can_order and can_delete.
Reported by: | Antoliny | Owned by: | Antoliny |
---|---|---|---|
Component: | Documentation | Version: | 5.1 |
Severity: | Normal | Keywords: | Formset |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In the example from can_order, calls to is_valid() are unnecessary. because ordered_forms, a property that is accessed later, calls the is_valid method.
@property def ordered_forms(self): """ Return a list of form in the order specified by the incoming data. Raise an AttributeError if ordering is not allowed. """ if not self.is_valid() or not self.can_order: raise AttributeError( "'%s' object has no attribute 'ordered_forms'" % self.__class__.__name__ ) ...
And I think the order of dictionary keys in the output of the examples for can_order and can_delete should be fixed.
As far as I know that the order of the keys is affected by the fields defined in ArticleForm.
Therefore, the fields of the ArticleForm used in the Formset documentation examples should follow the key order -> title, pub_date. ...
However, the output in the examples for can_order and can_delete does not seem to follow that order.
can_order
>>> formset = ArticleFormSet( ... data, ... initial=[ ... {"title": "Article #1", "pub_date": datetime.date(2008, 5, 10)}, ... {"title": "Article #2", "pub_date": datetime.date(2008, 5, 11)}, ... ], ... ) >>> formset.is_valid() True >>> for form in formset.ordered_forms: ... print(form.cleaned_data) ... {'pub_date': datetime.date(2008, 5, 1), 'ORDER': 0, 'title': 'Article #3'} {'pub_date': datetime.date(2008, 5, 11), 'ORDER': 1, 'title': 'Article #2'} {'pub_date': datetime.date(2008, 5, 10), 'ORDER': 2, 'title': 'Article #1'}
can_delete
>>> formset = ArticleFormSet( ... data, ... initial=[ ... {"title": "Article #1", "pub_date": datetime.date(2008, 5, 10)}, ... {"title": "Article #2", "pub_date": datetime.date(2008, 5, 11)}, ... ], ... ) >>> [form.cleaned_data for form in formset.deleted_forms] [{'DELETE': True, 'pub_date': datetime.date(2008, 5, 10), 'title': 'Article #1'}]
Change History (5)
comment:1 by , 2 hours ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:2 by , 2 hours ago
comment:3 by , 2 hours ago
Has patch: | set |
---|
comment:4 by , 19 minutes ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:5 by , 15 minutes ago
Triage Stage: | Accepted → Ready for checkin |
---|
PR