#35964: Remove unnecessary calls and fix dictionary key order in Formset
documentation examples can_order and can_delete.
--------------------------+------------------------------------------------
Reporter: Antoliny | Type: Cleanup/optimization
Status: new | Component: Documentation
Version: 5.1 | Severity: Normal
Keywords: Formset | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------+------------------------------------------------
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'}]
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/35964>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.