How to query number of input based on minute in Django?
Clash Royale CLAN TAG#URR8PPP
How to query number of input based on minute in Django?
So i'm implementing a webapps for a local trucking rental SME. basically what it does is it logs date, hour and minutes to count the Ritase(the output) of the truck.
here is models.py
class Muatan(models.Model):
recorded_date = models.DateField(auto_now_add=True)
date_added = models.DateField(auto_now_add=False)
shift = models.ForeignKey(Shift, on_delete=models.DO_NOTHING)
time_logged = models.TimeField(auto_now_add=False)
excavator = models.ForeignKey(Excavator, on_delete=models.DO_NOTHING)
operator_excavator = models.ForeignKey(ExcavatorOperator, on_delete=models.DO_NOTHING)
dumpTruck = models.ForeignKey(DumpTruck, on_delete=models.DO_NOTHING, null=True)
driver_dumptruck = models.ForeignKey(DumpTruckDriver, on_delete=models.DO_NOTHING)
location = models.ForeignKey(Lokasi, on_delete=models.DO_NOTHING)
material = models.ForeignKey(Material, on_delete=models.DO_NOTHING)
reported_problem = models.TextField(blank=True)
to give an example from the models, suppose that the manager logs these input from admin.py. simplified for question purposes, i only put date_added and time_logged.
the expected output from the query would be something like
because there are 3 logs within the hour 14.
how would i implement this in django? also, do i put the code in models.py or views.py? total django noobs here, really appreciate your help SO :)
1 Answer
1
I manage to find the answer after digging to the documentation. it uses the TruncHour
function
TruncHour
muatan.objects.annotate(hour=TruncHour('time_logged', output_field=TimeField()),).values('hour').annotate(muatan=Count('id'))
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.