First, please read Format of requests and responses documentation. The format of the response of flask-restless
is different from what you desire.
If you were to use flask-restless
, currently it is not possible to preload Deal._company._employees
(only level 1 relationships can be loaded). In your case you could register endpoint at Company
though, which will load both Company._deal
as well as Company._employees
:
api_manager.create_api(
Company, collection_name="custom_company",
results_per_page = -1, # disable pagination completely
)
then, doing:
rv = c.get('/api/custom_company_api',
headers={'content-type': 'application/json'},
)
will return something like:
{
"num_results": XXX,
"objects": [
{
"_deal": {
"_company_id": 1,
"active": true,
"id": 1
},
"employees": [
{
"_company_id": 1,
"id": 1,
"username": "User1"
},
{
"_company_id": 1,
"id": 2,
"username": "User2"
}
],
"id": 1,
"name": "Company1"
},
{
...
}
I believe, this is all you can with flask-restless at this point. In case you were to provide your own custom endpoint, then you can get all the data in one SQL
statement and convert to your desired format on your own. sqlalchemy query might look like this:
from sqlalchemy.orm import joinedload
qry = (db.session.query(Deal)
.options(
joinedload(Deal.company).
joinedload(Company.employees)
)
.filter(Deal.active == True)
)
Note, however, that this will only work if your _employees
relationship is not "lazy='dynamic'"