nastyn8 nastyn8 - 8 months ago 94
Python Question

When to override get method in Django CBV?

I've been learning Django and one source of confusion I have is with class based views and when to override the get method. I've looked through the documentation and it explains what get does but it doesn't explain when I should override get.

I originally created a view this way:

class ExampleView(generic.ListView):
template_name = 'ppm/ppm.html'
paginate_by = 5

def get(self, request):
profiles_set = EmployeeProfile.objects.all()
context = {
'profiles_set': profiles_set,
'title': 'Employee Profiles'
return render(request, self.template_name, context)

But I was recently told that my code was simple of enough for the default implementation, and that all I needed was this:

class ExampleView(generic.ListView):
model = EmployeeProfile
template_name = 'ppm/ppm.html'

So my Question is this: In what scenario/circumstance should I override the get method?


If you are using the builtin generic views, then you should rarely have to override get(). You'll end up either duplicating lots of functionality, or break features of the view.

For example, the paginate_by option will no longer work in your view, because you are not slicing the queryset in your get() method.

If you are using a generic class based view like ListView, you should try to override specific attributes or methods where possible, rather than overriding get().

The advantage of your view which overrides get() is that it's very clear what it does. You can see that the view fetches a queryset, includes it in a context, then renders the templates. You don't need to know about ListView to understand the view.

If you like the explicitness of overriding get() subclass View instead. You aren't using any of the features of ListView, so it doesn't make sense to subclass it.

from django.views.generic import View

class ExampleView(View):
    template_name = 'ppm/ppm.html'

    def get(self, request):