Opened 7 years ago

Closed 6 years ago

Last modified 5 years ago

#12475 closed (fixed)

Confusion between list_display and list_display_links for 'id' field

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

Description

In a ModelAdmin class, define list_display with 'id' and some other fields.
If 'id' is the first field, define list_display_link with some fields but without 'id',
otherwise you can omit list_display_link.

Problem : The 'id' column is rendered as a second hidden <input> (the 'correct' hidden input is still at the end of the <tr>) and the value is not shown.

Workaround : To have a correct screen when 'id' is in list_display, it has to be in list_display_link as well (either explicity or when a default list_display_link takes it as the first field of list_display).

Attachments (2)

12475_changelist_hidden_fields.diff (5.0 KB) - added by Julien Phalip 6 years ago.
12475_changelist_hidden_fields.2.diff (6.4 KB) - added by Julien Phalip 6 years ago.

Download all attachments as: .zip

Change History (16)

comment:1 Changed 7 years ago by szczav

I presume that this strange behaviour is caused by the fact that you have used "list_display_link" instead of "list_display_links" ('s' at the end). It is no bug, but small oversight :)

comment:2 Changed 7 years ago by Russell Keith-Magee

Resolution: invalid
Status: newclosed

comment:3 Changed 7 years ago by master

Resolution: invalid
Status: closedreopened
Summary: Confusion between list_display and list_display_link for 'id' fieldConfusion between list_display and list_display_links for 'id' field
Version: 1.11.2-beta

szczav: where did you find such a "list_display_link" function ? It was only a typo.
Please read "list_display_links" in my description.

Note: Problem was detected on v1.1, and can still be reproduced on v1.2 beta 1.

comment:4 Changed 7 years ago by Karen Tracey

Resolution: worksforme
Status: reopenedclosed

I cannot recreate any problem with id being include in list_display and list_display_links, on r13253. Given this model:

class TestModel(models.Model):
    name = models.CharField(max_length=40)
    birthdate = models.DateField(null=True, blank=True)
    birthtime = models.TimeField(null=True, blank=True)

The following model admin definition produces a correct change list page for TestModel:

class TMAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'birthdate', 'birthtime')
    list_display_links = ('name',)

The ID for the model is shown, but is not a link to the change page for the instance; the name field is shown and is a link to the change page, the other two fields are displayed but not links.

If I include id in list_display_links that works as well (the id value then also becomes a link to the change page), and if I omit list_display_links entirely then everything is still fine: in that case the id value is the one and only link to the change page for the instance.

If you continue to see a problem on current code, please be more specific about your model and model admin definitions: given the current description I cannot find any problem.

comment:5 Changed 7 years ago by master

Resolution: worksforme
Status: closedreopened

I confirm that your demo classes don't show the problem. I discovered that they will (for me, r13265) with an editable field (anyone valid, i.e. in list_display and not in list_display_links):

list_editable = ('birthtime',)

comment:6 Changed 7 years ago by Karen Tracey

Triage Stage: UnreviewedAccepted
Version: 1.2-beta1.1

OK, problem exists if you have the model admin specified like so, for example:

class TMAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'birthdate', 'birthtime')
    list_display_links = ('name',)
    list_editable = ('birthtime', )

And the nature of the problem is that while there is a column shown for ID values, nothing appears in it.

Testing shows this problem has existed as far back as 1.1; it was not introduced in the 1.2 cycle.

comment:7 Changed 6 years ago by Karen Tracey

#13825 reported this again.

comment:8 Changed 6 years ago by Julien Phalip

Has patch: set
milestone: 1.3

Thanks Karen and master for narrowing down the test case. In fact the issue is that if there is a form (i.e. list_editable is activated) and the form contains some hidden fields (in this case 'id') and those fields are not in list_editable or in list_display_links, then the hidden fields are displayed in the table cell instead of its human-readable value. The attached patch fixes this problem by never showing hidden fields in the table, since those fields are already taken care of separately in <div class="hiddenfields"> anyway.

Changed 6 years ago by Julien Phalip

comment:9 Changed 6 years ago by Simon Meers

I'm not sure how robust this solution is; from my skimming of the code, it appears that result_hidden_fields only adds hidden fields for the model's primary key; if we remove all hidden fields from the table, does this mean that non-pk hidden fields (if such things are possible in list-editing?) will disappear altogether?

Changed 6 years ago by Julien Phalip

comment:10 Changed 6 years ago by Julien Phalip

Thank you Simon. You're right, result_hidden_fields only deals with the primary keys. Other non-pk hidden fields are "displayed" normally in the changelist table. I have amended the patch and the tests to reflect that.

comment:11 Changed 6 years ago by Simon Meers

Triage Stage: AcceptedReady for checkin

That looks better, thanks Julien.

comment:12 Changed 6 years ago by Jannis Leidel

Resolution: fixed
Status: reopenedclosed

In [15722]:

Fixed #12475 -- Fixed an edge case with hidden fields in ModelAdmin changelists when used in conjunction with list_display_links or list_editable. Thanks, Simon Meers, Julien Phalip, Karen and master.

comment:13 Changed 6 years ago by Jannis Leidel

In [15724]:

[1.2.X] Fixed #12475 -- Fixed an edge case with hidden fields in ModelAdmin changelists when used in conjunction with list_display_links or list_editable. Thanks, Simon Meers, Julien Phalip, Karen and master.

Backport from trunk (r15722).

comment:14 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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