Opened 7 years ago

Last modified 10 months ago

#8165 new New feature

When using can_order=True on formset, ordered_forms is not avaible when formset.is_valid() is false

Reported by: James Chua <james_027@…> Owned by: zain
Component: Forms Version: master
Severity: Normal Keywords: formsets
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

IMO ordered_forms attribute needs to be available even then the formset.is_valid() result to false in order to return the formset in order for re-inputting purpose

Change History (17)

comment:1 Changed 7 years ago by James Chua <james_027@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

The error is

raise AttributeError("'%s' object has no attribute 'ordered_forms'" % self.class.name)

comment:2 Changed 7 years ago by James Chua <james_027@…>

  • Owner changed from nobody to brosner

comment:3 Changed 7 years ago by jacob

  • milestone set to 1.0
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 7 years ago by brosner

  • milestone changed from 1.0 to post-1.0
  • Resolution set to invalid
  • Status changed from new to closed

I talked with Joseph about this. He says that the behavior of ordered_forms is completely intentional to mirror how cleaned_data works. I can't seem to remember exactly what we discussed here James. I apologize. I am going to close for now, but you are welcome to reopen with a specific use-case. At any rate it is a post-1.0 item due to feature freeze and this isn't a bug.

comment:5 Changed 7 years ago by james_027

  • Resolution invalid deleted
  • Status changed from closed to reopened

An specific use-case could be if a page with a number of same forms were submitted and not every forms are valid and needed to return for re-inputting purpose and when it needed to be return with an order needed, ordered_forms should be available in spite of is_valid() is false.

comment:6 Changed 7 years ago by patrickk

I absolutely agree with james_027. why should ordered_forms only be available when is_valid() is true. it may be intentional to mirror cleaned_data, but that doesn´t mean it´s a good idea.

comment:7 Changed 7 years ago by patrickk

another usecase: ordering edit-inlines should be possible when order_with_respect_to is defined (or when you use a custom formset with can_order=true). in case of an error, the order of the items (edited inline) is obviously wrong.
solving this whole issue would allow for re-ordering edit-inlines.

comment:8 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:9 Changed 6 years ago by zain

  • milestone set to 1.2
  • Owner changed from brosner to zain
  • Status changed from reopened to new

comment:10 Changed 6 years ago by zain

  • Status changed from new to assigned

comment:11 Changed 5 years ago by dantallis

I believe ticket:11801 addresses this issue, and that ticket includes a patch and tests.

comment:12 Changed 5 years ago by jacob

  • Resolution set to duplicate
  • Status changed from assigned to closed

Dup of #11801.

comment:13 Changed 4 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

comment:14 Changed 4 years ago by yakiimo

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

It seems to me that this is not actually resolved in the ticket this is marked as a duplicate of. Could someone tell me why I'm wrong? I'm new with django so I don't want to reopen it unnecessarily.

The other ticket seems to be on a related issue, but in 1.3 ordered_forms are still not available before the form passes is_valid().

In formsets.py, line 184, within BaseFormSet:

    def _get_ordered_forms(self):
        """
        Returns a list of form in the order specified by the incoming data.
        Raises 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__)
        ...

For what it's worth, my use case is that I'd like to have parallel ordering between two formsets through a foreign key. The only two ways I found were through overriding iter or this way. I thought this way seemed more natural since it doesn't require any overrides.

comment:15 Changed 4 years ago by carljm

  • Resolution duplicate deleted
  • Status changed from closed to reopened
  • Type changed from Uncategorized to New feature

Yes, this ticket was incorrectly closed as a duplicate. #11801 addressed a much more edge case: when there is a deleted form that would otherwise have been invalid, but since it is deleted the overall formset is still valid. This ticket is requesting that the ordered_forms attribute should be available unconditionally, even when the formset is actually not valid.

Thanks yakiimo for pointing this out.

Last edited 4 years ago by carljm (previous) (diff)

comment:16 Changed 2 years ago by aaugustin

  • Status changed from reopened to new

comment:17 Changed 10 months ago by timgraham

#21592 is a duplicate which has an incomplete patch and some discussion.

Note: See TracTickets for help on using tickets.
Back to Top