How To Fetch An Entire Row In A Table And Display It In A Template
I am trying to make a ticketing site for a Cinema which my project in school, and I made two models, 1 is Movie and 2 TimeSlot. Movie.time_slot has a manytomanyrelation to TimeSlot
Solution 1:
If you look at the many-to-many example in the docs, you could access to time slots using movie_instance.time_slot.all()
.
In your view you could omit the TimeSlot
queryset:
def home(request):
context = {
'movielist': Movie.objects.all(),
'title': pagetitle
}
for m in context['movielist']: # for debug, remove later
print(m.time_slot.all()) # for debug, remove later
return render(request, 'movies/home.html', context)
And in your template you can try
<strong>Time Slots:</strong>
{% for t in movie.time_slot.all %}
{{ t.timeslot1 }}
{{ t.timeslot2 }}
...
{% endfor %}
Or you could add a method to your model to filter and prepare a string with what you need and then just call that method in your template for each movie instance.
class Movie(models.Model):
def get_monday_time_slots(self):
return self.time_slot.filter(day='monday')
Does that help?
Post a Comment for "How To Fetch An Entire Row In A Table And Display It In A Template"