You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在aiogram FSM中重置状态且不清除已存储的数据

aiogram 2.x 保留数据重置状态的解决方法

你可以直接给state.finish()方法传入reset_data=False参数实现需求:该参数默认值为True,即执行时默认清空所有已存储的用户数据,设置为False后方法只会将用户状态重置为初始状态,不会删除已存储的数据。

修改后的代码示例如下:

if user_data.get("chosen_groups") == "fired":
    # some code here
    await state.finish(reset_data=False)

你也可以使用等价的底层方法state.reset_state()实现相同效果,该方法需传入with_data=False参数:

await state.reset_state(with_data=False)

上述两种写法均适配你使用的aiogram 2.16版本,无需额外调整状态定义,不会降低代码可读性。

内容的提问来源于stack exchange,提问作者Ccorrilab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 16:57:00