使用 datetime 与 Django 中的日期进行比较
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题出在这行中,
它将从字面上查看
payment_date 是否现在。我认为您想查看现在是否大于或等于
payment_date ,在这种情况下您应该使用
您也可以在查询数据库时过滤发票:
更新
您的代码是错误的,因为您具有互斥的条件。看:
首先检查
payment_date 是否早于现在。然后它将
owing
设置为invoice_gross
。 然后,在相同的条件中,它检查payment_date 是否晚于现在。但那不可能!仅当
payment_date 早于现在时,您才处于此代码块中!
我认为你有一个缩进错误,并且想要这个:
当然,这与以下内容相同:
I think the problem is in the line
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 thepayment_date
, in which case you should useYou can also just filter the invoices when you query the database:
Update
Your code is wrong because you have mutually exclusive conditionals. Look:
That first checks to see if
payment_date
is before now. Then it setsowing
toinvoice_gross
. Then, in the same conditional, it checks to see ifpayment_date
is after now. But that can't be! You are only in this block of code ifpayment_date
is before now!I think you have an indentation error, and want this instead:
Which, of course, is the same as:
使用
datetime.now()
(注意括号)。除此之外,请记住该字段始终是一个datetime
对象。另外,(我猜)您应该仅检查日期时间的日期以匹配当前日期(否则它只会匹配特定的秒)。为此,您必须检查是否payment_date.date() == date.today()
(其中date
是datetime.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 adatetime
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 ifpayment_date.date() == date.today()
(wheredate
isdatetime.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>
感谢穆沙希德·汗
https://stackoverflow.com/a/37596662/12575117
(短代码)
你可以使用这个:
Thank to Mushahid Khan
https://stackoverflow.com/a/37596662/12575117
(Short Code)
You can use this one: