Hi all, I’m pulling my hair out to figure out how I can make this query within a class-based generic view.
´´´select b.book_name, s.title, s.id
from books_books b, books_sessoes s
where b.id = 3 AND b.sessaoid_id = s.id´´´
If I run this query I get:
The where b.id=3 should be the book id variable from the context within the view, here is the view:
´´´class DetalhesLivro(DetailView):
model = Books
template_name = ‘books/detalhe_livro.html’
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context ['sessao'] = Sessoes.objects.values('title')
return context´´´
this is the template that calls the template “books/detalhe_livro.html”
´´´{% for livros in sessoes.books_set.all %}
{% endfor %}´´´
the url file:
´´´app_name = ‘book’
urlpatterns = [
path(‘’, views.IndexView.as_view(), name=‘index’),
path(‘about/’, views.about, name=‘Books-About’),
path(‘int:pk/’, views.DetailView.as_view(), name=‘detalhe’),
path(‘detalhe_livro/int:pk/’, views.DetalhesLivro.as_view(), name=‘detalhe_livro’),
]´´´
and this is the model:
´´´class Sessoes(models.Model):
title = models.CharField(max_length=100)
sessao_date = models.DateField()
def str(self):
return self.title
class Books(models.Model):
sessaoid = models.ForeignKey(Sessoes, on_delete=models.CASCADE)
book_name = models.CharField(max_length=100)
description = models.CharField(max_length=250)
sinopse = models.TextField()
image = models.ImageField(upload_to=‘books/’)´´´
the result I get is all the values in the Seesao table and I only want the tittle for the specific Sessao of that Book
Any help would be much appreciated.