1

I have the following schema:

class PublisherSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        fields = ('name',)
        model = Publisher


class JournalSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        fields = ('title', 'publisher')
        model = Journal
        ordered = True

    publisher = ma.Nested(PublisherSchema)

When I dump the JournalSchema I want the result to be:

{
  "title": "hello",
  "publisher: "bye"
}

But right now it dumps as:

{
  "title": "hello",
  "publisher": {
    "name": "bye"
  }
}

How can I nest the publisher value but not display the key?

Casey
  • 2,611
  • 6
  • 34
  • 60

1 Answers1

0

Actually I figured it out. The way to do it is:

class JournalSchema(ma.SQLAlchemyAutoSchema):
    publisher = fields.String(attribute="publisher.name")

    class Meta:
        fields = ('title', 'publisher')
        model = Journal
        ordered = True

This considers that 'publisher' is a relationship of the referenced model.

Casey
  • 2,611
  • 6
  • 34
  • 60