Reputation: 1398
timetable_choices =(
('Monday','Monday'),
('Tuesday','Tuesday'),
('Wednesday','Wednesday'),
('Thursday','Thursday'),
('Friday','Friday'),
('Saturday','Saturday'),
)
class Timetable(models.Model):
day = models.ForeignKey('Day',on_delete=models.CASCADE,blank=True,null=True)
start = models.IntegerField()
end = models.IntegerField()
subject = models.CharField(max_length=12)
class Day(models.Model):
day = models.CharField(max_length=9, choices=timetable_choices)
I have already populated the Day table and Timetable table.
class Timetabletemplate(TemplateView):
template_name = 'timetable.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['timetable'] = Timetable.objects.all()
context['monday'] = Timetable.objects.filter(day="Monday")
return context
<table style="width:100%">
<tr>
<th></th>
<th>1</th>
</tr>
<tr>
<td>Monday</td>
{% for item in monday %}
<td>{{item.subject}}</td>
{% endfor %}
</tr>
</table>
What I am trying to do is render subject in a cell.I have n subjects, number of subjects depend on how many subjects the user has added . I want the subjects when day = Monday in the first row and similarly when day = Tuesday,wednesday in the respective rows below monday .
I am getting an error
invalid literal for int() with base 10: 'Monday'
when I tried this code .
Upvotes: 0
Views: 53
Reputation: 27503
context['monday'] = Timetable.objects.filter(day__day="Monday")
as you are using ForeignKey
to Day
model, the days are save as pk
and not the choice name
Upvotes: 2