Answer the question
In order to leave comments, you need to log in
How to pass object id from context to get_context_data()?
I have two models:
class Project(BaseModel):
slug = models.SlugField(verbose_name='URL', unique=True, blank=False)
category = models.ManyToManyField(Category, verbose_name='Category')
title = models.CharField(max_length=200, verbose_name='Title', blank=False)
publication = models.BooleanField(verbose_name='Publication', default=True)
class Screenshot(BaseModel):
image = models.ImageField(upload_to='uploads/projects/%Y/%m/', verbose_name='Image', blank=False)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
class ProjectDetailView(DetailView):
model = Project
template_name = 'portfolio/project_detail.html'
context_object_name = 'project'
queryset = Project.objects.filter(publication=True)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['screenshots'] = Screenshot.objects.filter(project=???) # id from Project object
return context
Answer the question
In order to leave comments, you need to log in
Like so:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['screenshots'] = Screenshot.objects.filter(project=self.object) # id from Project object
return context
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question