Remove deprecated uses of prefetch_related from views #6643

Closed
opened 2025-12-29 19:43:26 +01:00 by adam · 6 comments
Owner

Originally created by @hagbarddenstore on GitHub (Jul 11, 2022).

Originally assigned to: @hagbarddenstore on GitHub.

Proposed Changes

In views which display their data in a table, the use of prefetch_related can be removed since the table itself prefetches the displayed columns internally.

This is the code doing the magic:
https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/tables/tables.py#L76

Example of code that can be removed:
https://github.com/netbox-community/netbox/blob/develop/netbox/circuits/views.py#L194

Justification

Calling prefetch_related on some views but not others, adds confusion to developers whom are fixing bugs or developing new features, as it is unclear when it's needed or not.

Originally created by @hagbarddenstore on GitHub (Jul 11, 2022). Originally assigned to: @hagbarddenstore on GitHub. ### Proposed Changes In views which display their data in a table, the use of `prefetch_related` can be removed since the table itself prefetches the displayed columns internally. This is the code doing the magic: https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/tables/tables.py#L76 Example of code that can be removed: https://github.com/netbox-community/netbox/blob/develop/netbox/circuits/views.py#L194 ### Justification Calling `prefetch_related` on some views but not others, adds confusion to developers whom are fixing bugs or developing new features, as it is unclear when it's needed or not.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 19:43:26 +01:00
adam closed this issue 2025-12-29 19:43:27 +01:00
Author
Owner

@hagbarddenstore commented on GitHub (Jul 11, 2022):

(I discussed it with @kkthxbye-code on Slack)

I can be assigned on this.

@hagbarddenstore commented on GitHub (Jul 11, 2022): (I discussed it with @kkthxbye-code on Slack) I can be assigned on this.
Author
Owner

@jeremystretch commented on GitHub (Jul 11, 2022):

Bear in mind that some prefetching is still needed. The automatic prefetching looks at the accessor used for the table column, but isn't smart enough to know if additional data is needed. For example, a TemplateColumn might render data from an additional related object, in which case a manual call to prefetch that object in the queryset is still necessary.

@jeremystretch commented on GitHub (Jul 11, 2022): Bear in mind that _some_ prefetching is still needed. The automatic prefetching looks at the accessor used for the table column, but isn't smart enough to know if additional data is needed. For example, a TemplateColumn might render data from an additional related object, in which case a manual call to prefetch that object in the queryset is still necessary.
Author
Owner

@jeremystretch commented on GitHub (Jul 27, 2022):

@hagbarddenstore would you still like to work on this? If not, I can take care of it.

@jeremystretch commented on GitHub (Jul 27, 2022): @hagbarddenstore would you still like to work on this? If not, I can take care of it.
Author
Owner

@jeremystretch commented on GitHub (Aug 1, 2022):

Great work @hagbarddenstore, thank you!

@jeremystretch commented on GitHub (Aug 1, 2022): Great work @hagbarddenstore, thank you!
Author
Owner

@jeremystretch commented on GitHub (Aug 1, 2022):

#9886 has been merged into the feature branch.

@jeremystretch commented on GitHub (Aug 1, 2022): #9886 has been merged into the `feature` branch.
Author
Owner

@hagbarddenstore commented on GitHub (Oct 11, 2022):

I still want to do it. I’ve just been on vacation with the kids :)

Sent from my iPhone

On 27 Jul 2022, at 17:23, Jeremy Stretch @.***> wrote:


@hagbarddenstore would you still like to work on this? If not, I can take care of it.


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.

@hagbarddenstore commented on GitHub (Oct 11, 2022): I still want to do it. I’ve just been on vacation with the kids :) Sent from my iPhone > On 27 Jul 2022, at 17:23, Jeremy Stretch ***@***.***> wrote: > >  > @hagbarddenstore would you still like to work on this? If not, I can take care of it. > > — > Reply to this email directly, view it on GitHub, or unsubscribe. > You are receiving this because you were mentioned.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#6643