Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in nested inline not bubbling up #128

Open
leibowitz opened this issue Jun 18, 2021 · 3 comments
Open

Error in nested inline not bubbling up #128

leibowitz opened this issue Jun 18, 2021 · 3 comments

Comments

@leibowitz
Copy link
Contributor

leibowitz commented Jun 18, 2021

I am using django-nested-inline with a model defined like this:

Person
└─ House
    └─Room

My Admin is defined in this way:

from nested_inline.admin import NestedStackedInline, NestedModelAdmin

class RoomInlineStacked(NestedStackedInline):
    ...

class HouseInline(NestedStackedInline):
    inlines = [RoomInlineStacked]

class PersonAdmin(NestedModelAdmin):
    inlines = [HouseInline]

The form displays correctly, and works fine, until there is an error in the RoomInlineStacked. In which case, the error is only displayed within the inline formset, but not at the top level (where errors are normally shown)

Errors within HouseInline are reported correctly at the top of the page. I'm expecting this to happen for the RoomInlineStacked as well. aka I'm expecting to see this (which doesn't happen at the moment):
Screenshot 2021-06-18 at 11 19 10

I am trying to understand what's missing within AdminErrorList in https://github.com/s-block/django-nested-inline/blob/master/nested_inline/admin.py#L264 but I'm struggling to navigate the structure of formsets/forms/inlines and other attributes to find the error from the underlying NestedStackedInline and send it back up to the NestedModelAdmin.

Any pointers appreciated

@leibowitz leibowitz changed the title Error in nested inline not bubbling up Error in multi-level nested inline not bubbling up Jun 18, 2021
@leibowitz leibowitz changed the title Error in multi-level nested inline not bubbling up Error in nested inline not bubbling up Jun 18, 2021
@fpoulain
Copy link

I am facing the same issue.

@fpoulain
Copy link

The problem is that helpers.AdminErrorList isn't builded to catch recursively errors (at arbitrary level) and it is not called with recursive formsets.

Since all_valid_with_nesting is walking recursively formsets, I suggest to backstep the error at this step.

@fpoulain
Copy link

My fix occurs in some private code but I simply redefined def all_valid_with_nesting in a subclass of NestedModelAdmin and added

             for form in formset:
                 if hasattr(form, 'nested_formsets'):
+                    if not self.all_valid_with_nesting(form.nested_formsets):
+                        # backstep error
+                        form.errors[None] = [
+                            f.errors + f.non_form_errors()
+                            for f in form.nested_formsets
+                        ]
                        return False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants