ПАТЧИРОВАТЬ ресурсы с вложенными объектами с помощью Flask / SQLAlchemy - PullRequest
0 голосов
/ 30 мая 2018

У меня есть следующие настройки:

# models
class Author(BaseModel):
    id = Column(Integer, primary_key=True)
    first_name = Column(String(64))
    last_name = Column(String(64))

class Book(db.Model):
    id = Column(Integer, primary_key=True) 
    title = Column(String(64))
    author_id = Column(Integer, 
        ForeignKey("author.id"), nullable=True)
    author = relationship(Author, 
        backref=backref('books'))

# schema    
class AuthorSchema(BaseSchema):
    first_name = fields.Str()
    last_name = fields.Str()

    class Meta(BaseSchema.Meta):
        type_ = 'author'
        model = Author

class BookSchema(BaseSchema):
    title = fields.Str()
    author_id = fields.Int()
    author = fields.Nested('flask_and_restless.schemas.AuthorSchema', many=False)

    class Meta(BaseSchema.Meta):
        type_ = 'book'
        model = Book

Я могу отправить книгу с вложенным автором с такой полезной нагрузкой:

{
    "data": {
        "attributes": {
            "author": {
                "data": {
                    "attributes": {
                        "first_name": "author 2",
                        "last_name": "last 2"
                    },
                    "type": "author"
                }
            },
            "title": "my new title"
        },
        "type": "book"
    }
}

Это создает нового автора вместе с новой книгой,

Однако, когда я пытаюсь ПАТЧИРОВАТЬ с похожей полезной нагрузкой (тот же автор, другое название книги), как в

{
    "data": {
        "attributes": {
            "author": {
                "data": {
                    "attributes": {
                        "first_name": "author 2",
                        "last_name": "last 2"
                    },
                    "type": "author"
                }
            },
            "title": "updated title 3"
        },
        "id": "3",
        "type": "book"
    }
}

, я получаю: AttributeError: 'dict' object has no attribute '_sa_instance_state'

Мойстек: Flask-SQLAlchemy, Marshmallow-JSONAPI, Flask-Restless (полный демонстрационный источник здесь )

Есть идеи, как это исправить?

...