我刚开始学习django,想做一个简单的django应用来更好地了解它。在代码的某个位置,我需要挑选locationName
并获取与表中相同id匹配的元素。当我开始思考如何使用最pythonic的方式来跳出for循环时,我不禁想起了continue
。
下面是相关代码:
for locationName in locationGroup:
idRef = locationName.id
try:
element = location.objects.order_by('-id').filter(name__id=idRef)[0]
except IndexError:
continue
try
/catch
еқ— (pass
) дёӯвҖңи·іеҮәвҖқеҗ—пјҹиҝҳжҳҜд»ҺеҪ“еүҚеҫӘзҺҜиҝӯд»Јдёӯ (continue
) и·іеҮәпјҹжҲ–иҖ…жҳҜд»Һж•ҙдёӘеҫӘзҺҜ (break
) дёӯи·іеҮәпјҹ - user558061or
的旧方法要好得多。 - Voo