Using datetime to compare with dates in Django

I have a question in Django about how you can compare dates to solve some solutions. For example, I have a date field in my models. As shown below.

class Invoice(models.Model): payment_date = models.DateTimeField() 

What I want to do is ask if there is a way to compare date and time with DateTimeField. For example, if I had a list of payment dates, and I wanted to compare with datetime now. Payment indicators that are late with their payments are shown without fail. Otherwise, this value is zero.

Here are my views to show what is happening. I have tried so far, but I get a value of 0 for payment_date, which is later than the payment date.

Change here are my last views. It's funny that I seem to get owing = invoice_gross for all the results - unlike the previous ones, when I got all 0s. Therefore, 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 basically does something like this.

 ID Owing 1 100 (All the same value) 2 100 3 100 . . . . . . 
+7
source share
2 answers

I think the problem is in the line

 if datetime.now() == payment_date: 

This will literally see if there is payment_date right now. I think you want to see whether payment_date is greater than or equal payment_date , in which case you should use

 if datetime.now() >= payment_date: 

You can also simply filter invoices when querying the database:

 invoices_list = Invoice.objects.filter(payment_date__lte=datetime.now()) 

Update

Your code is incorrect because you have mutually exclusive conditions. Take a look:

 if payment_date <= datetime.now(): owing = invoice_gross if payment_date > datetime.now(): owing = 0 

First it checks to see if there is a payment_date before that. He then sets owing to invoice_gross . Then, in the same condition, it checks to see if payment_date thereafter. But this cannot be! You are only in this block of code if payment_date is still there!

I think you have an indented error, and instead want:

 if payment_date <= datetime.now(): owing = invoice_gross if payment_date > datetime.now(): owing = 0 

Which, of course, matches:

 if payment_date <= datetime.now(): owing = invoice_gross else: owing = 0 
+7
source

Use datetime.now() (note the parsers). In addition, remember that the field will always be a datetime object. In addition, (I assume) you should only check the date of the date and time so that it matches the current date (otherwise it will only correspond to a specific second). To do this, you need to check if payment_date.date() == date.today() (where date datetime.date ) is

It also means that you can filter it like this: Invoice.objects.filter(payment_date__lte=datetime.now()) .

__lte , __gte , __lt , __gt are used for <= , >= , < and >

+9
source

All Articles