美好的一天!如何正确地将FOR循环应用于给定函数?
感谢提示:)
from datetime import timedelta
from apps.tickets.models import PatientConsultationTicket
def tickets_change():
tickets = PatientConsultationTicket.object.all()
one_day = tickets.created_at + timedelta(days=1)
if datetime.now() > one_day:
tickets.status = 'rd'
tickets.save()
elif datetime.now() < one_day:
tickets.status = 'e'
tickets.save()
答案 0 :(得分:0)
要回答您的问题,那就是如何迭代门票:
from datetime import timedelta
from apps.tickets.models import PatientConsultationTicket
def tickets_change():
tickets = PatientConsultationTicket.objects.all()
for ticket in tickets:
one_day = ticket.created_at + timedelta(days=1)
if datetime.now() > one_day:
ticket.status = 'rd'
ticket.save()
elif datetime.now() < one_day:
ticket.status = 'e'
ticket.save()
尽管如此,它可能不是您想要做的最好的方法。 相反,您可能应该使用“ UPDATE”查询(未经测试,但看起来可能类似):
PatientConsultationTicket.objects.filter(created_at__gte=datetime.now()-timedelta(days=1)).update(status='rd')
PatientConsultationTicket.objects.filter(created_at__lte=datetime.now()-timedelta(days=1)).update(status='e')