In the basic example we use api.route(ComputerList, 'computer_list', '/computers', '/persons/<int:id>/computers'), which adds a route for fetching all computers (/computers), or all computers related to a person (/persons/<int:id>/computers).
However, if you create a simple ResourceList like this, requests to /persons/<int:id>/computers will return all computers, not just those associated with a certain person:
In the example, this is solved using a custom filter:
class ComputerList(ResourceList):
def query(self, view_kwargs):
query_ = self.session.query(Computer)
if view_kwargs.get('id') is not None:
try:
self.session.query(Person).filter_by(id=view_kwargs['id']).one()
except NoResultFound:
raise ObjectNotFound({'parameter': 'id'}, "Person: {} not found".format(view_kwargs['id']))
else:
query_ = query_.join(Person).filter(Person.id == view_kwargs['id'])
return query_
def before_create_object(self, data, view_kwargs):
if view_kwargs.get('id') is not None:
person = self.session.query(Person).filter_by(id=view_kwargs['id']).one()
data['person_id'] = person.id
However, there's no reason we can't automate this filtering, in the same way that we do for ResourceRelationship fields (although these are different). It's incredibly common to want `/persons/1/computers to return only the computers from person 1.
In the basic example we use
api.route(ComputerList, 'computer_list', '/computers', '/persons/<int:id>/computers')
, which adds a route for fetching all computers (/computers
), or all computers related to a person (/persons/<int:id>/computers
).However, if you create a simple
ResourceList
like this, requests to/persons/<int:id>/computers
will return all computers, not just those associated with a certain person:In the example, this is solved using a custom filter:
However, there's no reason we can't automate this filtering, in the same way that we do for
ResourceRelationship
fields (although these are different). It's incredibly common to want`/persons/1/computers
to return only the computers from person 1.