Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33029 closed New feature (fixed)

Can't open multiple Django Admin Popups for the same related field.

Reported by: Yash Jhunjhunwala Owned by: Yash Jhunjhunwala
Component: contrib.admin Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description (last modified by Carlton Gibson)

Django Admin shows popup for adding/editing a related field object. The popup doesn't always a new popup but instead replaces the current one when opening multiple popups.

Steps to recreate the problem:

For E.g.

class A(models.Model):
    foo = models.ForeignKey('self', on_delete=models.SET_NULL,null=True,blank=True,verbose_name="If True",related_name="foo_set",)
    bar = models.ForeignKey('self', on_delete=models.SET_NULL,null=True,blank=True,verbose_name="If True",related_name="bar_set",)

For the above Model A :

Create a new instance of model A using the admin. Open a relatedmodel popup for foo by clicking on the add sign.
In the new pop up click on the add sign for foo again and it will replace the current popup (instead of opening up another popup) whereas if you click on the add sign for bar it will open another popup - which is the expected behavior.

Change History (17)

comment:1 by Carlton Gibson, 3 years ago

Description: modified (diff)
Resolution: wontfix
Status: newclosed
Summary: Django Admin PopupCan't open multiple Django Admin Popups for the same related field.

Hi. Thanks for the report.

I'm going to say wontfix here.

In the normal case this works as expected. I'm adding a new object for a foreign key; I click the same + button; I can't add two objects to the same FK, so the popup is replaced; meanwhile, if I click the + for a second related field a new popup is created (since I can add two different objects to two different FKs).

The case for self is more complex, since in principle I could add a chain of models for the same FK, but each on a different instance. However, without a candidate implementation it's not clear it's worth additional complexity to the popup implementation to handle that case. (You can simply build the chain in the other direction.)

Happy to reopen if you want to work on an implementation that's not too complex though!
Thanks.

comment:2 by Carlton Gibson, 3 years ago

Type: BugNew feature

in reply to:  1 comment:3 by Yash Jhunjhunwala, 3 years ago

Replying to Carlton Gibson:

Happy to reopen if you want to work on an implementation that's not too complex though!

What if we add an index of sorts to the window name. for e.g. if the field name is foo then the instead of window name being "id_foo", we can use "id_foo_1". Also pass the index to popup.

I click the same + button; I can't add two objects to the same FK, so the popup is replaced;

This would work as is since the + will always open window of the same index. But would solve the problem for the chain of models.

comment:4 by Yash Jhunjhunwala, 3 years ago

Resolution: wontfix
Status: closednew

Created a draft pull request https://github.com/django/django/pull/14823

Need to add tests.

Last edited 3 years ago by Yash Jhunjhunwala (previous) (diff)

comment:5 by Carlton Gibson, 3 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted
Version: 2.23.2

Hi Yash. OK — the draft PR looks small enough to consider. I didn't yet check that it functions fully as expected but if that's all that it would take...

Need to add tests.

So for these you'll need to use Selenium. See the section in the contributing guide on running the selenium tests.

If you create an admin for a self referential model and check with a test case that a chain (say 3?) can be opened and filled in in order, and that they correctly function (and populate the right form back up the chain) then that shouldn't be too far off.

Thanks.

comment:6 by Yash Jhunjhunwala, 3 years ago

Owner: changed from nobody to Yash Jhunjhunwala
Status: newassigned

comment:7 by Yash Jhunjhunwala, 3 years ago

Added the tests and submitted a new pull request

comment:8 by Yash Jhunjhunwala, 3 years ago

Has patch: set

comment:9 by Yash Jhunjhunwala, 3 years ago

Needs tests: unset

comment:10 by Mariusz Felisiak, 3 years ago

Patch needs improvement: set

comment:11 by Yash Jhunjhunwala, 3 years ago

Patch needs improvement: unset

Fixed Pre Commit Linter issues

in reply to:  10 comment:12 by Yash Jhunjhunwala, 3 years ago

Replying to Mariusz Felisiak:
Running pre-commit shows all tests passed but linter workflow fails (isort) on GitHub

comment:13 by Carlton Gibson, 3 years ago

Patch needs improvement: set

Just some small tweaks to the behaviour and tests still needed. But patch look sound generally.

comment:14 by Yash Jhunjhunwala, 3 years ago

Patch needs improvement: unset

Updated the patch as per the changes suggested by Carlton

Version 0, edited 3 years ago by Yash Jhunjhunwala (next)

comment:15 by Carlton Gibson, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:16 by Carlton Gibson <carlton.gibson@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 492ed60:

Fixed #33029 -- Allowed multiple popups for self-related fields in admin.

comment:17 by Carlton Gibson <carlton@…>, 3 years ago

In 402ae378:

Refs #33029 -- Fixed popups Selenium tests in headless mode.

Co-authored-by: Yash Jhunjhunwala <yash@…>

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