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 Antoliny, 2 hours ago

Owner: set to Antoliny
Status: newassigned

comment:2 by Antoliny, 2 hours ago

comment:3 by Antoliny, 2 hours ago

Has patch: set

comment:4 by Sarah Boyce, 19 minutes ago

Triage Stage: UnreviewedAccepted

comment:5 by Sarah Boyce, 15 minutes ago

Triage Stage: AcceptedReady for checkin
Note: See TracTickets for help on using tickets.
Back to Top