当try-catch不起作用时,如何处理匹配查询不存在?

2024-09-30 14:16:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如果父技术不在数据库上,我想跳过添加子技术,以避免matching query does not exist.,但是如果我使用.first or .exists,即使使用if not ...也不起作用,或者try catch块保持来自Dmeo.objects.get_or_create(的相同错误,我如何解决这个问题

追溯

analizer         | apps.api.models.DoesNotExist: Dmeo matching query does not exist.

analizer         | The above exception was the direct cause of the following exception:
analizer         | 
analizer         | Traceback (most recent call last):
analizer         |   File "updater.py", line 38, in <module>
analizer         |     subtechnique, created = Dmeo.objects.get_or_create(

下降码

for subtechnique in src_subtechniques:
    for data_subtechniques in subtechnique:
        technique = Technique.objects.filter(technique_id=data_subtechniques['technique_id'].partition('.')[0]).exists()
        subtechnique, created = Dmeo.objects.get_or_create(
            subtechnique_id=data_subtechniques['technique_id'],
            technique_name=data_subtechniques['technique'],
            url=data_subtechniques['url'],
        )

Tags: oriniddatagetobjectscreatenot

热门问题