0

Is there a way to have Potion return an inline collection of json objects, instead of returing JSON Hyperschema references?

E.g. my current code is returning this:

{
"$uri": "/worker/1",
"mobile_number": null,
"name": "Ben",
"password": "Ben",
"rosters": [
    {
        "$ref": "/roster/1"
    }
]
}

But I would like it to return

    {
"$uri": "/worker/1",
"mobile_number": null,
"name": "Ben",
"password": "Ben",
"rosters": [
    {
    "$uri": "/roster/1",
    "name": "21"
    }
]
}

So basically to eager load all the oneToMany roster objects and inline them in the JSON? Basically to avoid having to do multiple calls back to the server to lazily load the $ref documents?

DaBeeeenster
  • 1,481
  • 3
  • 16
  • 20

1 Answers1

0

There are no problem with it: just use: fields.Inline() method instead of: fields.ToOne()

Xander Luciano
  • 3,753
  • 7
  • 32
  • 53
  • Welcome to stack overflow! When posting code on SO you should either surround it in \` for inline code (the key under the squiggly key aka the tilde) or indent it with 4 spaces for blocks of code. – Xander Luciano Jun 29 '16 at 19:42
  • Excuse me, I missed, that you need a collection inlined. I copy-pasted class `fields.ToMany()`, and changed `filds.ToOne()` to `fields.Inline()`: ` class Inlines(fields.Array): """ Like :class:`Inline`, but for arrays of references. """ def __init__(self, resource, **kwargs): super(Inlines, self).__init__(fields.Inline(resource, nullable=False), **kwargs) fields.Inlines = Inlines ` – Daniel Samsyguin Jun 30 '16 at 04:57
  • You can edit your answer to include that comment so it's a little easier to read :) – Xander Luciano Jun 30 '16 at 21:51
  • @DanielSamsyguin No need for a custom class. You can use `fields.Array(fields.Inline('roster'))` – lyschoening Sep 28 '16 at 14:36