gpt4 book ai didi

python-2.7 - 如何使用棉花糖在sqlalchemy中序列化枚举属性

转载 作者:行者123 更新时间:2023-12-04 11:00:54 26 4
gpt4 key购买 nike

这是我的模特类

class Type(enum.Enum):
Certified = "certified"
Non_Certified = "non-certified"


class Status(enum.Enum):
Approved = "Approved"
Rejected = "Rejected"
Published = "Published"
Retired = "Retired"
Waiting_for_Approval = "Waiting_for_Approval"


class DifficultyLevel(enum.Enum):
Beginner = "Beginner"
Intermediate = "Intermediate"
Advanced = "Advanced"
All = "All"


class ActiveStatus(enum.Enum):
Archive = "archive"
Restore = "restore"


class Course(Base):
__tablename__ = 'course'
id = Column(Integer, primary_key=True)
course_code = Column(String(255))
duration_in_hours = Column(Integer)
default_eb_price = Column(Integer)
modified_by = Column(Integer)
modified_on = Column(DateTime)
created_by = Column(Integer)
created_at = Column(DateTime)
type = Column(Enum(Type))
certification_vendor = Column(String(255))
certification_name = Column(String(255))
micro_training_sessions = Column(Integer)
status = Column(Enum(Status))
title = Column(String(255))
summary = Column(String(255))
duration = Column(JSON)
# Categories =
delivery_language = Column(String(255))
course_logo = Column(String(255))
promo_video = Column(String(255))
overview = Column(String(255))
objectives = Column(String(255))
suggested_attendees = Column(String(255))
prerequisites = Column(String(255))
difficulty_level = Column(Enum(DifficultyLevel))
course_facts = Column(JSON)
price = Column(String(255))
list_price = Column(String(255))
early_bird_price = Column(String(255))
next_recommended_course = Column(postgresql.ARRAY(Integer))
specialization_paths = Column(postgresql.ARRAY(Integer))
active_status = Column(Enum(ActiveStatus))

def __getitem__(self, item):
return getattr(self, item)

@property
def serialize(self):
"""Return object data in easily serializeable format"""
serialized_obj = {}
for column in self.__table__.columns:
serialized_obj[column.key] = self[column.key]
return serialized_obj

这是 Controller 功能来编辑更改
def update_course_ctrl(obj,course_id):
args = request.args
course_schema = CourseSchema()
if args is not None :
if args['action'] == "archive":
course = session.query(Course).filter_by(id=course_id).one()
course.active_status = 'Archive'
session.merge(course)
session.commit()
dump_data = course_schema.dump(course).data
return dump_data
# code to archive course
if args['action'] == "restore":
course = session.query(Course).filter_by(id=course_id).one()
course.active_status = 'Restore'
session.merge(course)
session.commit()
dump_data = course_schema.dump(course).data
return dump_data
course = Course(**obj.json)
session.merge(course)
session.commit()
dump_data = course_schema.dump(course).data
return dump_data

这是我的棉花糖文件中的代码
from marshmallow_sqlalchemy import ModelSchema
from sqlalchemy.orm import sessionmaker
from app.extensions import engine
from app.course.models import Course, Project, Topic, Resource


DBSession = sessionmaker(bind=engine)
session = DBSession()


class CourseSchema(ModelSchema):
class Meta:
model = Course
sqla_session = session

在调用此更新函数时出现此错误
类型错误: 不是JSON可序列化的

最佳答案

对于枚举字段,最简单的方法是通过pip(marshmallow_enum)安装名为pip install marshmallow_enum的软件包,将其导入项目中,然后在棉花糖架构定义中使用自定义定义覆盖SQLAlchemy字段:

from marshmallow_enum import EnumField
...


class CourseSchema(ModelSchema):
type = EnumField(Type, by_value=True)

class Meta:
model = Course
sqla_session = session

自定义定义未覆盖的所有字段都将从模型中获取。

您可以尝试使用 by_value参数,该参数使您可以序列化枚举的名称(默认情况下)或值( by_value=True)。

关于python-2.7 - 如何使用棉花糖在sqlalchemy中序列化枚举属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44717768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com