使用 datetime 与 Django 中的日期进行比较

发布于 2024-10-10 23:53:59 字数 1233 浏览 3 评论 0原文

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

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

我想要做的是询问这是否是一种将 datetime.now 与 DateTimeField 进行比较的方法。例如,如果我有一个付款日期列表,并且我想与现在的日期时间进行比较。延迟付款的 payment_date 将显示在欠款中。否则,该值为零。

这是我的观点,以展示正在发生的事情。到目前为止我已尝试过,但 payment_date 的值为 0,该值晚于付款日期。

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

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

哦,我的桌子基本上就是做这样的事情。

ID  Owing
1   100   (All the same value)
2   100
3   100
.   .
.   .
.   .

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

季末如歌 2024-10-17 23:53:59

我认为问题出在这行中,

if datetime.now() == payment_date:

它将从字面上查看 payment_date 是否现在。我认为您想查看现在是否大于或等于 payment_date ,在这种情况下您应该使用

if datetime.now() >= payment_date:

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

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

更新

您的代码是错误的,因为您具有互斥的条件。看:

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

首先检查 payment_date 是否早于现在。然后它将 owing 设置为 invoice_gross然后在相同的条件中,它检查payment_date 是否晚于现在。但那不可能!仅当 payment_date 早于现在时,您才处于此代码块中!

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

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

I think the problem is in the line

if datetime.now() == 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

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

Which, of course, is the same as:

if payment_date <= datetime.now():
    owing = invoice_gross
else:
    owing = 0
弱骨蛰伏 2024-10-17 23:53:59

使用datetime.now()(注意括号)。除此之外,请记住该字段始终是一个datetime对象。另外,(我猜)您应该仅检查日期时间日期以匹配当前日期(否则它只会匹配特定的秒)。为此,您必须检查是否 payment_date.date() == date.today() (其中 datedatetime.date

这也是意味着您可以像这样进行过滤:Invoice.objects.filter( payment_date__lte=datetime.now())

__lte__gte__lt__gt 用于 <=>=<>

Use datetime.now() (notice the parens). Other than that, remember that the field will always be a datetime object. Also, (I guess that) you should check only the date of the datetime to match the current date (or else it will only match that specific second). For that you have to check if payment_date.date() == date.today() (where date is datetime.date)

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

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

吐个泡泡 2024-10-17 23:53:59

感谢穆沙希德·汗
https://stackoverflow.com/a/37596662/12575117

(短代码)
你可以使用这个:

from django.utils.timezone import datetime

Thank to Mushahid Khan
https://stackoverflow.com/a/37596662/12575117

(Short Code)
You can use this one:

from django.utils.timezone import datetime
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文