I have two models i.e Starttime and Stoptime that store the start time and stop time respectively. Right now, each registered user will have multiple instances of the start and stop time, like so:
Name Start_time Stop_time
Bobby Dec. 31, 2019, 5:39 a.m Dec. 31, 2019, 5:50 a.m
Jan. 01, 2020, 9:00 a.m Jan. 01, 2020, 18:00 a.m
Jan. 02, 2020, 6:00 a.m Jan. 02, 2020, 19:00 a.m
... ...
Tina Dec. 31, 2019, 9:00 a.m Dec. 31, 2019, 10:00 a.m
Dec. 31, 2019, 12:00 p.m Dec. 31, 2019, 15:00 p.m
Jan. 01, 2020, 9:00 a.m Jan. 01, 2020, 11:00 a.m
Jan. 02, 2020, 5:00 a.m Jan. 02, 2020, 9:00 a.m
Jan. 02, 2020, 10:00 a.m Jan. 02, 2020, 12:00 a.m
... ...
I want to display my data exactly like this within the HTML table.
models.py:
class Starttime(models.Model):
user_id= models.ForeignKey(User, on_delete = models.CASCADE)
start_time = models.DateTimeField()
class Stoptime(models.Model):
user_id= models.ForeignKey(User, on_delete = models.CASCADE)
stop_time = models.DateTimeField()
views.py:
#Right now, I am accessing only the latest values from both the models. However, I want to display all of them neatly in my HTML table.
def interface(request):
data = User.objects.filter(pk__gt=1) #All users apart from the SuperUser admin
store_data = []
for user in data:
sta_time = Starttime.objects.filter(user_id = user)
sta_data = sta_time.values_list('start_time', flat=True).latest('start_time')
sto_time = Stoptime.objects.filter(user_id = user)
sto_data = sto_time.values_list('stop_time', flat=True).latest('stop_time')
store_data.append((user.first_name, sta_data, sto_data))
return render(request, 'users/interface.html', {'data': store_data})
interface.html:
<table>
<tr>
<th> Name</th>
<th> Start Time </th>
<th> Stop Time </th>
</tr>
{% for column in data %}
<tr>
<td>{{column.0}}</td>
<td>{{column.1}}</td>
<td>{{column.2}}</td>
</tr>
{% endfor %}
</table>
However, within my views.py, I am only accessing the latest start and stop times in the HTML table. But, my aim is to display all the time logs for each user.
How can I display all the time objects respectively in each column within the HTML table and is there a way to check if the time data for both my models are empty or not?
Thank you! :)