An alternative approach would be to override the get_object method of the get_object subclass, something along the line:
class CurrentUserDetailView(UserDetailView): def get_object(self): return self.request.user
Significantly cleaner, simpler, and more class-based representations than the mixin method.
EDIT. To clarify, I believe that two different URL patterns (i.e. one with pk and the other without) should be defined separately in urlconf. Therefore, they can be served by two different types, especially since this makes the code cleaner. In this case, urlconf might look something like this:
urlpatterns = patterns('', url(r"^users/(?P<pk>\d+)/$", UserDetailView.as_view(), name="user_detail"), url(r"^users/current/$", CurrentUserDetailView.as_view(), name="current_user_detail"), url(r"^users/$", UserListView.as_view(), name="user_list"), )
And I updated my example above to note that it inherits from UserDetailView , which makes it even cleaner and makes it clear that this is in fact: a special case of the parent view.
source share