Omit get() method from simplistic ObjectView subclasses #4240

Closed
opened 2025-12-29 18:34:11 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Nov 3, 2020).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

The ObjectView class provides a basic get() method which renders a template with the current object as its context. Some views still employ their own minimal get() method which duplicate this functionality unnecessarily. For example:

class CableView(ObjectView):
    queryset = Cable.objects.all()

    def get(self, request, pk):
        cable = get_object_or_404(self.queryset, pk=pk)
        return render(request, 'dcim/cable.html', {
            'cable': cable,
        })

The get() method above can be omitted and the template updated to reference instance rather than cable.

Justification

Removes unneeded code

Originally created by @jeremystretch on GitHub (Nov 3, 2020). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes The ObjectView class provides a basic `get()` method which renders a template with the current object as its context. Some views still employ their own minimal `get()` method which duplicate this functionality unnecessarily. For example: ```python class CableView(ObjectView): queryset = Cable.objects.all() def get(self, request, pk): cable = get_object_or_404(self.queryset, pk=pk) return render(request, 'dcim/cable.html', { 'cable': cable, }) ``` The `get()` method above can be omitted and the template updated to reference `instance` rather than `cable`. ### Justification Removes unneeded code
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:34:11 +01:00
adam closed this issue 2025-12-29 18:34:12 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4240