小编Tee*_*ter的帖子

SQLAlchemy 问题 - Marshmallow 嵌套对象推断外键

我试图让 Marshmallow-SQLAlchemy 反序列化具有嵌套对象的对象,而不指定嵌套对象的外键(应该是父对象的主键)。这是一个独立的示例:

# Python version == 3.8.2
from datetime import datetime
import re

# SQLAlchemy == 1.3.23
from sqlalchemy import func, create_engine, Column, ForeignKey, Text, DateTime
from sqlalchemy.ext.declarative import as_declarative, declared_attr
from sqlalchemy.orm import relationship, sessionmaker

# marshmallow==3.10.0
# marshmallow-sqlalchemy==0.24.2
from marshmallow import fields
from marshmallow.fields import Nested
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema

################################################################################
# Set up
################################################################################

engine = create_engine("sqlite:///test.db")

Session = sessionmaker()
Session.configure(bind=engine)
session = Session()


################################################################################
# Models
################################################################################

@as_declarative()
class Base(object):

    @declared_attr
    def __tablename__(cls):
        # From …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy marshmallow marshmallow-sqlalchemy

1
推荐指数
1
解决办法
1947
查看次数