I have a model with a time_stamp (DateTime field) and I want to get the latest entry of every month.
How can I approach this problem?
Model:
class Transaction (models.Model):
transaction_id = models.AutoField(primary_key=True)
net_monthly_transaction = models.DecimalField(max_digits = 10, decimal_places = 2, default=0)
# deposit or withdrawal (withdrawal with negative value)
amount = models.DecimalField(max_digits = 10, decimal_places = 2)
time_stamp = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self): # __unicode__ on Python 2
return str(self.time_stamp) + str(self.amount) + str(self.net_monthly_transaction)
This can easily be accomplished in mysql which I am familiar with, and there are lots of answers for them in SO, here's an example:
SELECT * FROM table
WHERE created_on in
(select DISTINCT max(created_on) from table
GROUP BY YEAR(created_on), MONTH(created_on))
Get last record of each month in MySQL....?
How can I accomplish this in Django? Any help or direction would be appreciated.
Thanks in advance,