Python-使用SQLModel中的过渡状态机

发布于 2025-02-13 00:47:18 字数 957 浏览 1 评论 0原文

我想在我的SQLModel模式中使用状态计算机,但我会遇到类型错误。

class Invoice(BaseSQLModel, table=True):
  id: Optional[int] = Field(primary_key=True)
  def __init__(self):
     super().__init__()
     default_status = fsm.initial_payment_state
     self.fsm = fsm.get_payment_fsm(default_status). # Throws an error here because fsm is not an attribute of the class

是否可以声明创建列时忽略的属性?如何在此处声明FSM和状态属性?

我已经尝试过,但是当试图调用fsm.add_model(self)时,代码被卡住了

class Invoice(BaseSQLModel, table=True):
  id: Optional[int] = Field(primary_key=True)
  _fsm: "Machine" = PrivateAttr()

  def __init__(self):
     super().__init__()
     default_status = fsm.initial_payment_state
     self._fsm = fsm.get_payment_fsm(default_status)
     self._fsm.add_model(self). # Gets stuck here
    

    # Allowing me to declare additional attributes
    class Config:
        extra = Extra.allow
        arbitrary_types_allowed = True

I want to use a state machine in my SQLModel schema but I'm getting type errors.

class Invoice(BaseSQLModel, table=True):
  id: Optional[int] = Field(primary_key=True)
  def __init__(self):
     super().__init__()
     default_status = fsm.initial_payment_state
     self.fsm = fsm.get_payment_fsm(default_status). # Throws an error here because fsm is not an attribute of the class

Is it possible to declare an attribute which is ignored when creating columns? How can I declare the fsm and state attributes here?

I've tried this but then the code gets stuck when trying to call fsm.add_model(self)

class Invoice(BaseSQLModel, table=True):
  id: Optional[int] = Field(primary_key=True)
  _fsm: "Machine" = PrivateAttr()

  def __init__(self):
     super().__init__()
     default_status = fsm.initial_payment_state
     self._fsm = fsm.get_payment_fsm(default_status)
     self._fsm.add_model(self). # Gets stuck here
    

    # Allowing me to declare additional attributes
    class Config:
        extra = Extra.allow
        arbitrary_types_allowed = True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文