django中嵌套的try-except實(shí)例
我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧!
# 因?yàn)榇藭r(shí)為yaml模板,而且只抓取node port,所以這樣處理效率快content_dict = parse_yaml(content.replace('{{', '').replace('}}', ''))if ’service’ in content_dict.keys(): # 記錄本次yaml里所有的node_port,并更新到數(shù)據(jù)庫(kù) now_app_list = [] for service_item in content_dict[’service’]: for port_item in service_item[’port’]: if ’nodePort’ in port_item.keys():node_port = int(port_item[’nodePort’])if 30000 <= node_port <= 32000: # 這里判斷比較復(fù)雜,如果端口有,而且app相同,不更新。如果端口有,app不一樣,報(bào)沖突。如果端口沒(méi)有,可插入。 now_app_list.append(node_port) try: AppPort.objects.get(node_port=node_port, app=app) pass except AppPort.DoesNotExist: try: AppPort.objects.get(node_port=node_port) messages.info(self.request, ’nodeport{}端口沖突!’.format(node_port)) return HttpResponseRedirect(reverse_lazy('app:yaml_edit', kwargs=self.kwargs)) except AppPort.DoesNotExist: name = ’{}-{}’.format(app.name, node_port) AppPort.objects.create(name=name,app=app,node_port=node_port )else: messages.info(self.request, ’nodeport{}端口不在指定范圍內(nèi)(30000-~32000)!’.format(node_port)) return HttpResponseRedirect(reverse_lazy('app:yaml_edit', kwargs=self.kwargs)) # 取出AppPort里所有此app的node_port,多余的要清除。 all_app_list = AppPort.objects.filter(app=app).values_list(’node_port’, flat=True) if all_app_list: # 取交集,也就是數(shù)據(jù)庫(kù)里多余的端口列表 diff_list = [x for x in all_app_list if x not in now_app_list] if diff_list: AppPort.objects.filter(node_port__in=diff_list).delete()
感覺(jué)上面這段代碼,應(yīng)用的技術(shù)點(diǎn)蠻多的,作個(gè)記錄。
包括其node port的管理思想,提取技巧。
orm的列表扁平化,列表交集,批量刪除
補(bǔ)充知識(shí):Django 在異常捕獲中進(jìn)行數(shù)據(jù)庫(kù)保存,保存后將異常再拋出
需求:
當(dāng)我檢查配額時(shí),如果配額不足,我需要將訂單狀態(tài)改為5,然后再將原有異常拋出
一:最初做法
try: self.check_quota_enough(create_form) # 檢查配額except Exception as e: self.object.state = 5 self.object.save() raise e
存在問(wèn)題:django默認(rèn)開(kāi)啟了事務(wù),因此我保存訂單狀態(tài)后,再拋異常時(shí)沒(méi)有任何問(wèn)題
但當(dāng)我查詢(xún)數(shù)據(jù)庫(kù)訂單狀態(tài)時(shí),發(fā)現(xiàn)異常拋出之前訂單改變的狀態(tài)已經(jīng)丟失
二:優(yōu)化,使用裝飾器,手動(dòng)關(guān)閉事務(wù)
# 在dispatch方法上增加裝飾器,關(guān)閉事務(wù)class xxxxxxxx(): @method_decorator(transaction.non_atomic_requests) def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs) def form_valid(self, form): ... ... try: self.check_quota_enough(create_form) # 檢查配額 except Exception as e: self.object.state = 5 self.object.save() raise e
該方法可以解決上述問(wèn)題
但該方法對(duì)整個(gè)視圖有效,如果在視圖其他地方需要用到事務(wù)的一致性,則顯然不合理
二:優(yōu)化,定義事務(wù)保存點(diǎn),在局部控制事務(wù)一致性
class xxxxxxxx(): def form_valid(self, form): ... ... sid = transaction.savepoint() # 定義事務(wù)保存點(diǎn) try: self.check_quota_enough(create_form) except QuotaNotEnough as e: self.object.state = 5 self.object.save() transaction.savepoint_commit(sid) return self.render_to_json_response(result=False, messages=str(e))
a.save():此處保存了數(shù)據(jù)
sid = transaction.savepoint():設(shè)置保存點(diǎn)
b.save():此處再次保存了數(shù)據(jù)
接下來(lái)就是邏輯處理:如果滿(mǎn)足xxxxxxxx條件,則手動(dòng)提交,此時(shí)a.save()和b.save()保存的數(shù)據(jù)都會(huì)生效。
如果不滿(mǎn)足xxxxxxxx條件,則執(zhí)行事務(wù)回滾,此時(shí)我們定義的保存點(diǎn)之前的a.save()數(shù)據(jù)不會(huì)收到影響,但在保存點(diǎn)之后的b.save()的數(shù)據(jù)將會(huì)回滾。
所以我們可以通過(guò)調(diào)整保存點(diǎn)的位置,實(shí)現(xiàn)我們的需求。
以上這篇django中嵌套的try-except實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程2. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. 一個(gè) 2 年 Android 開(kāi)發(fā)者的 18 條忠告5. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼6. Spring的異常重試框架Spring Retry簡(jiǎn)單配置操作7. Android studio 解決logcat無(wú)過(guò)濾工具欄的操作8. 什么是Python變量作用域9. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析10. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼
