使用日期时间与 Django 中的日期进行比较 [英] Using datetime to compare with dates in Django

查看:45
本文介绍了使用日期时间与 Django 中的日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Django 中有一个关于如何比较日期以解决某些解决方案的问题.例如,我的 models.py 中有一个日期字段,如下所示.

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()

我想要做的是询问是否可以将 datetime.now 与 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.

这是我的观点,以显示正在发生的事情.到目前为止,我已经尝试过,但我得到了比付款日期晚的 payment_date 的 0 值.

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.

这里编辑的是我的最新观点.有趣的是,我似乎得到了所有结果的欠款 = invoice_gross - 不像以前我得到全 0.所以它仍然不能正常工作.

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
.   .
.   .
.   .

推荐答案

我觉得问题出在线路上

if datetime.now() == payment_date:

从字面上看,payment_date 是否是现在.我想你想看看现在是否大于或等于 payment_date,在这种情况下你应该使用

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

if datetime.now() >= payment_date:

您也可以在查询数据库时过滤发票:

You can also just filter the invoices when you query the database:

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

更新

你的代码是错误的,因为你有互斥的条件.看:

Update

Your code is wrong because you have mutually exclusive conditionals. Look:

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

首先检查 payment_date 是否在现在之前.然后它将owing 设置为invoice_gross.然后在相同的条件中,它会检查payment_date是否在现在之后.但那不可能!如果 payment_datebefore 现在,你才在这个代码块中!

That first checks to see if payment_date is before now. Then it sets owing to invoice_gross. Then, in the same conditional, it checks to see if payment_date is after now. But that can't be! You are only in this block of code if payment_date is before now!

我认为你有一个缩进错误,想要这个:

I think you have an indentation error, and want this instead:

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

这当然是一样的:

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

这篇关于使用日期时间与 Django 中的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆