简体   繁体   中英

Is there a more succinct for this date checking?

Every invoice may have several items and the invoice due date is the date of the earliest due item. Here's what I have but I wonder if there may be a shorter version:

due_date = None

for item in i.item_set.all():
    if due_date is None:
        due_date = item.due_date
    else:
        if due_date > item.due_date:
            due_date = item.due_date

Have you tried min ?

due_date = min((item.due_date for item in i.item_set.all()))

Or, another option:

from operator import attrgetter
due_date = min(i.item_set.all(), key=attrgetter("due_date")).due_date

Hope that helps.

由于您使用的是Django,因此可以这样操作:

invoice_date = i.item_set.order_by('due_date')[0].due_date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM