Skip to content

Conversation

jnovinger
Copy link
Member

Fixes: #18845

  • Restores sort order using Device.name column on DeviceTable.name table column.

This was a regression introduced in bf836c9 (in v4.2.5).

@jnovinger jnovinger requested review from a team and jeremystretch and removed request for a team March 10, 2025 22:52
class DeviceTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable):
name = tables.TemplateColumn(
verbose_name=_('Name'),
accessor=Accessor('label'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this might also need to be changed?

Suggested change
accessor=Accessor('label'),
accessor=Accessor('name'),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggested here: #18809

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of switching the accessor to name, it should be removed entirely. It was added in bf836c9 to display a value other than name.

I believe removing this line and switching the template DEVICE_LINK from value to record.label might preserve displaying the label but fix sorting. Sorry I didn't catch this before.

template_code=DEVICE_LINK,
linkify=True
linkify=True,
order_by=('name',)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed if we correct the accessor.

class DeviceTable(TenancyColumnsMixin, ContactsColumnMixin, NetBoxTable):
name = tables.TemplateColumn(
verbose_name=_('Name'),
accessor=Accessor('label'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @mraerino points out, the accessor needs to be reverted to name.

@jnovinger
Copy link
Member Author

@mraerino @jeremystretch , thanks for pointing that out. I had interpreted the previous change (that caused this behavior) to be intentional with respect to the display value of the object's Name column value.

Reverting the accessor argument reverts the behavior of using Device.label value for the Name column. See screenshots below, first is with my change, the second is with the suggested changes to this PR. In the second, you can see that devices belonging to the Tenant: NC State, Site: MDF, Location: Row 1 lose their descriptive value in the Name column and simply become "Unnamed Device".

Behavior based on this PR as filed:
image

Behavior with PR suggested changes:
image

I just want to clarify, this is the behavior we want--that is, these devices show up with "Unnamed Device" in the Name column?

@jeremystretch
Copy link
Member

I just want to clarify, this is the behavior we want--that is, these devices show up with "Unnamed Device" in the Name column?

Yep, that's okay. The "Unnamed device" label signifies that name is null for the device (which is permitted).

@alehaa
Copy link
Contributor

alehaa commented Mar 12, 2025

The "Unnamed device" label signifies that name is null for the device (which is permitted).

@jeremystretch the intention of #17357 was exactly to change this behavior and show the same display name as in the rack elevations view. I proposed a change to fix sorting but preserving #17357 in the comments above: #18861 (comment)

@jeremystretch
Copy link
Member

The column accessor should be set to a concrete value in the table data. If you want the column to display something other than the accessor's value, you need to use TemplateColumn or override the columns' render()/value() methods.

However, be aware that rendering attributes of any related objects which have not been prefetched in the querysey may trigger additional queries.

Thanks to @alehaa for the suggestion.

This also includes an additional `.select_related()` operation on
`DeviceListView.queryset` to avoid extra queries. Thanks to
@renatoalmeidaoliveira and @jeremystretch for pointing out the need for
this.
@jnovinger jnovinger requested a review from jeremystretch March 12, 2025 19:19
accessor=Accessor('label'),
template_code=DEVICE_LINK,
linkify=True
linkify=True,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, forgot I added this when adding the previous order_by argument. I'm a monster!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cvitan! The new guy is sneaking in commas again! I told you he was going to be trouble.

@jeremystretch jeremystretch merged commit 78332d4 into main Mar 13, 2025
6 checks passed
@jeremystretch jeremystretch deleted the 18845-device-sort-name-broken branch March 13, 2025 13:23
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot sort Devices by device NAME

4 participants