I have a question in Django on how you can compare dates to solve some solutions. For example I have a datefield in my models.py Like below.
class Invoice(models.Model):
payment_date = models.DateTimeField()
What I want to be able to do is ask if the is a way to compare a datetime.now with a DateTimeField. For example, if I had a list of payment dates and I wanted to compare with datetime now. Thhe payment_date's that are late with their payments are shown in owing. Otherwise, it the value is zero.
Here is my views to show whats going on. I have tried so far but I get a 0 value for payment_date's which are later than the payment date.
Edit here is my latest views. Funny thing is that I seem to be getting the owing = invoice_gross for all results - unlike before when I was getting all 0s. So it is still not working properly.
@login_required
def homepage(request):
invoices_list = Invoice.objects.all()
invoice_name = invoices_list[0].client_contract_number.client_number.name
invoice_gross = invoices_list[0].invoice_gross
payment_date = invoices_list[0].payment_date
if payment_date <= datetime.now():
owing = invoice_gross
if payment_date > datetime.now():
owing = 0
return render_to_response(('index.html', locals()), {'invoices_list': invoices_list ,'invoice_name':invoice_name, 'invoice_gross':invoice_gross,'payment_date':payment_date,'owing':owing}, context_instance=RequestContext(request))
Oh and my table is basically doing something like this.
ID Owing
1 100 (All the same value)
2 100
3 100
. .
. .
. .