我有许多代表不同版本字母的对象。其中一些版本已印刷(加盖时间戳)。如果已经打印了一封信(及其所有版本),我需要获取最后一个打印版本的时间戳(很容易完成),然后是最后一个打印版本的版本号(目前使我的代码看起来像 C++{shiver} )。
那么我如何让这个看起来更像pythonic(更干净)
try:
# get the lastest letter version that has been printed
lv_temp = LV.objects.filter(letter=letter.id,printed_last__isnull=False).latest('id')
# get the id's of all the letter versions for a particular letter
lv_temp2 = LV.objects.filter(letter=letter.id).order_by('id')
lv_temp4 = []
# get all the letter version for a particular letter
for lv_temp3 in lv_temp2:
lv_temp4.append(lv_temp3.id)
# get an array of the indexes and the pks
for i,v in enumerate(lv_temp4) :
# if the pk of the last printed version is the same one as in the loop...
if lv_temp.id == v :
# ...save the index as the version number
lv_printed_ver = i
lv_printed = lv_temp.printed_last
except ObjectDoesNotExist:
lv_printed = None
lv_printed_ver = None
(我用过lv_temp...
是因为我生气了多少次我不得不传递东西)