I use class-based view to update my model:
class BlogUpdateView(UpdateView):
model=Blog
def get_context_data(self, **kwargs):
context = super(BlogUpdateView, self).get_context_data(**kwargs)
context['author'] = get_object_or_404(User,
username__iexact=self.kwargs['username'])
return context
I want to follow the DRY principle and avoid repeating get_context_data
in every view function. The question is nearly the same as this one. Relying on the answer given by @Jj I suppose my class to look like this:
class BlogMixin(object):
def get_author(self):
# How to get author?
return author
def get_context_data(self, **kwargs):
context = super(BlogMixin, self).get_context_data(**kwargs)
context['author'] = self.get_author()
return context
My question is: How can I access object in mixin class?
UPDATE
Answer is given in mongoose_za comment. I can get author with this line:
author = User.objects.get(username__iexact=self.kwargs['username'])