1

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! :)

2 Answers 2

1

Make a Key and a value pair like this

store_data.append({
 'first_name' : user.first_name, 
 'sta_data': sta_data, 
 'sto_data' : sto_data
})

And In Template use it like this:

{% for column in data %}
    <tr>
        <td>{{column.first_name}}</td>
        <td>{{column.sta_data}}</td>
        <td>{{column.sto_data}}</td>
    </tr>
    {% endfor %}
Sign up to request clarification or add additional context in comments.

3 Comments

What if you have a lot of logs for each user (as shown in the example)? How can you structure the data in the HTML table? My table places all time log in one cell for each user. I want to expand it to each row as shown in the example.
You mean to say per row you wish to filter different user and show their time log?If this is the condition then you need to modify your query and you need to user itertools.groupby You need to get the time log groupby the user...
Can you please demonstrate a small example, please?
0

In my opinion you need to restructure your models :

class Timeentry(models.Model):
    user_id= models.ForeignKey(User, on_delete = models.CASCADE)
    start_time = models.DateTimeField()
    stop_time = models.DateTimeField(null=True, blank=True)

Only this way you will now what end time belongs to what start time - if there is two entries a day (see your example on Dec. 31).

Then you can simply retrieve the data and handle empty values:

for user in data:
    sta_time = Timeentry.objects.filter(user_id = user).valueslist("start_time","stop_time")
    sta_time_corrected=[]
    for sti in sta_time:#build table and replace empty values
        sta_time_corrected.append( [user.first_name]+["No Time" if x=="" else x for x in sti])
    store_data.append(sta_time_corrected)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.