Skip to content

Instantly share code, notes, and snippets.

@volgoweb
Created August 18, 2017 19:55
Show Gist options
  • Save volgoweb/e17da690a6e82e9a5b96cb7c45af1bc7 to your computer and use it in GitHub Desktop.
Save volgoweb/e17da690a6e82e9a5b96cb7c45af1bc7 to your computer and use it in GitHub Desktop.
Example of view in Django
class LotListView(ListView):
model = Article
context_object_name = 'articles'
def get_context_data(self, **kwargs):
context = super(LotListView, self).get_context_data(**kwargs)
context['articles_data'] = [self._get_article_view_model(a) for a in context['articles']]
return context
@staticmethod
def _get_article_view_model(article):
author = article.author.get_full_name()
view_model = dict(
title=article.title,
author_name=author_name,
)
return view_model
def get_queryset(self):
qs = self.model.objects.active_articles()
return qs
def get_form(self):
uqs = User.objects.filter(current_contractor=self.request.user.contractor)
return self.form_class(self.request.GET, contractor_users_qs=uqs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment