使用datetime来比较Django中的日期 [英] Using datetime to compare with dates in Django

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

问题描述

Django中有一个问题,您可以如何比较日期来解决一些问题。例如,我的models.py中有一个datefield,如下所示。

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。例如,如果我有一个付款日期列表,我现在想和datetime进行比较。付款迟到的付款日期显示为欠款。否则,它的值为零。

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.

这里编辑是我最新的观点。有趣的是,我似乎正在得到所有结果的due = invoice_gross - 与以前当我得到所有的0s不同。所以它仍然不能正常工作。

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 之前。然后,将 设置为 invoice_gross 但那不可能!如果 payment_date 之前,您只能在这个代码块中。

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如果pay_date< = datetime.now():$ b $($)

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

当然, as:

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

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

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