Welcome to the Treehouse Community
Looking to learn something new?
Hadi Farhat10,678 Points
How to know when to use super() when overriding a CVB method?
In the video, Kenneth overrode two CBV methods:
class TeamCreate(CreateView): fields = ("name", "coach", "practice_location") model = models.Team def get_initial(self): initial = super().get_initial() initial['coach'] = self.request.user.pk return initial
class TeamDelete(DeleteView): model = models.Team success_url = reverse_lazy('teams:list') def get_queryset(self): if not self.request.user.is_superuser: return self.model.objects.filter(coach=self.request.user) return self.model.objects.all()
The difference between them is calling
super() and I want to know when to call it.
Steven Parker221,297 Points
It looks like more a matter of "why" than "when". The first function needs to access "initial" and uses super to call the get_initial method of the parent class.
The second one doesn't need access to anything other than what it inherits so it has no need to use super.