Skip to content

Instantly share code, notes, and snippets.

@slomo
Created June 22, 2016 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slomo/0d75e9a95f5f37673411a83c867c5c96 to your computer and use it in GitHub Desktop.
Save slomo/0d75e9a95f5f37673411a83c867c5c96 to your computer and use it in GitHub Desktop.
# projects/mixins.py
class ProjectMixin():
def get_project(self):
pass
def get_context_data(self, **kwargs):
kwargs['project'] = self.get_project()
return super().get_context_data(**kwargs)
# modules/views.py
from django.utils.functional import cached_property
from django.views.generic import ListView
from . import models
class ModuleListView(ProjectMixin, ListView):
model = models.Module
def get_project(self):
return self.object_list.first().project
# Alternative
# @property
@cached_property
def project(self):
"""
Benutzung im Template:
{{ view.project }}
"""
return self.object_list.first().project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment