Reputation: 1181
I have a table that holds VM analytic data, every time the data is entered it inserts a time-stamp. The data is entered every hour and truncated at the 30day mark. I need to write a Django query to search a given amount of days based on a posted integer from a customer.
Thus the customer submits the number 7 and the the Django query returns the last 7 days data. I was looking into the documentation about dates but it doesn't seem like it will do what I want.
I really just need some help here, on where to look or even if its possible with Django.
This is my current query:
vms = VmStatHistories.objects.filter(customer_id = customer_id).order_by('vm_id').distinct('vm_id').filter(datetime = datetime.datetime.today()-datetime.timedelta(days=number_of_days))
The models field:
class VmStatHistories(models.Model):
vm_stat_id = models.AutoField(primary_key=True, editable=False)
vm_id = models.BigIntegerField()
customer = models.ForeignKey(Customers)
vm_name = models.CharField(max_length=4000)
read_request = models.DecimalField(max_digits=15, decimal_places=2)
write_request = models.DecimalField(max_digits=15, decimal_places=2)
cpu_usage_ghz = models.DecimalField(max_digits=15, decimal_places=2)
memory_consumed_mb = models.DecimalField(max_digits=15, decimal_places=2)
memory_active_mb = models.DecimalField(max_digits=15, decimal_places=2)
memory_swap_mb = models.DecimalField(max_digits=15, decimal_places=2)
bytes_sent_kbps = models.DecimalField(max_digits=15, decimal_places=2)
bytes_received_kbps = models.DecimalField(max_digits=15, decimal_places=2)
disk_usage_mbps = models.DecimalField(max_digits=15, decimal_places=2)
ip_address = models.CharField(max_length=16)
provisioned_ram_gb = models.BigIntegerField()
provisioned_cores = models.BigIntegerField()
provisioned_storage_gb = models.BigIntegerField()
consumed_storage_gb = models.BigIntegerField()
datetime = models.DateTimeField()
class Meta:
managed = True
db_table = 'vm_stat_histories'
def __unicode__(self): # Python 3: def __str__(self):
return self.vm_name
Upvotes: 0
Views: 671
Reputation: 37364
Your base query is almost right. Just use datetime__gte=datetime.datetime.today() -datetime.timedelta(days=number_of_days)
in your filter
call instead of datetime=...
. As written, you're asking for data timestamped at exactly the specified number of days ago, not data from that number of days up until the present.
Upvotes: 2