问了半天 ai 也没解决。
class ApprovalStatus(PyEnum):
PENDING = 'pending'
APPROVED = 'approved'
REJECTED = 'rejected'
class Approval(db.Model):
id = db.Column(db.Integer, primary_key=True)
content_type = db.Column(db.String(50), nullable=False)
content_id = db.Column(db.Integer, nullable=False)
field_name = db.Column(db.String(50), nullable=False)
new_value = db.Column(db.Text, nullable=False)
status = db.Column(db.Enum(ApprovalStatus), default=ApprovalStatus.PENDING)
submitter_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
submit_time = db.Column(db.DateTime, default=current_time)
reviewer_id = db.Column(db.Integer, db.ForeignKey('user.id'))
review_time = db.Column(db.DateTime, default=current_time)
review_comment = db.Column(db.Text)
submitter = db.relationship('User', foreign_keys=[submitter_id])
reviewer = db.relationship('User', foreign_keys=[reviewer_id])
__table_args__ = (db.UniqueConstraint('content_type', 'content_id'),)
@property
def content(self):
model = getattr(models, self.content_type)
return model.query.get(self.content_id)
模型是这样,但是在 flask admin 访问审核这个栏目的时候,总是提示
LookupError: 'pending' is not among the defined enum values. Enum name: approvalstatus. Possible values: PENDING, APPROVED, REJECTED
和解哦。
结果检查,数据库的状态栏的值都是小写的。
1
lovekernel 89 天前
你是否最近对数据库进行了迁移或修改?
可以尝试检查数据库迁移工具(如 Flask-Migrate )是否正确处理了枚举类型。 |
2
nonozone OP 已经有了啊。数据库我也查询了,状态都是小写的:
``` # revision identifiers, used by Alembic. revision = '616677b3122c' down_revision = 'b96ad24de868' branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('approval', sa.Column('id', sa.Integer(), nullable=False), sa.Column('content_type', sa.String(length=50), nullable=False), sa.Column('content_id', sa.Integer(), nullable=False), sa.Column('submitter_id', sa.Integer(), nullable=False), sa.Column('reviewer_id', sa.Integer(), nullable=True), sa.Column('status', sa.Enum('pending', 'approved', 'rejected', name='approval_status'), nullable=True), sa.Column('submit_time', sa.DateTime(), nullable=True), sa.Column('review_time', sa.DateTime(), nullable=True), sa.Column('review_comment', sa.Text(), nullable=True), sa.ForeignKeyConstraint(['reviewer_id'], ['user.id'], ), sa.ForeignKeyConstraint(['submitter_id'], ['user.id'], ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('content_type', 'content_id') ) with op.batch_alter_table('user', schema=None) as batch_op: batch_op.add_column(sa.Column('avatar', sa.String(length=120), nullable=True)) # ### end Alembic commands ### ``` |
3
nevermoreluo 88 天前
是我太久不写 py 了嘛,我不记得有个 PyEnum 的类型,先确认下你自己用的 orm 再确定继承的枚举父类对不对吧。
|
4
nonozone OP @nevermoreluo
from enum import Enum as PyEnum 不过现在即使改成: class ApprovalStatus(enum.Enum): PENDING = 'pending' APPROVED = 'approved' REJECTED = 'rejected' 也一样。 总之就是明明数据库里实际的值都是小写,但是好像处理的时候,总体提示说应该大写。 除非不用这种方法。 vote_status = db.Column(Enum('open', 'closed', 'auto', name='weekly_vote_status_enum'), default='auto') 这样写倒是没问题 |
5
zhufpy 88 天前
PENDING = 'PENDING'
APPROVED = 'APPROVED' REJECTED = 'REJECTED' |