Created
June 22, 2016 07:40
-
-
Save slomo/0d75e9a95f5f37673411a83c867c5c96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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