3

I have an api to a database with 2 models. The models have a relationship as follows:

  • Every product is owned by one entity. (many-1)
  • Every entity owns 1 or more products. (1-many)

These are defined in the models.py file as follows:

class Product(db.Model):
    some_other_attributies = redacted
    entity_id              = db.Column(db.Integer, db.ForeignKey('entity.id'))


class Entity(db.Model):
    id       = db.Column(db.Integer, primary_key = True)
    name     = db.Column(db.String(64), unique=True)
    # Relationships
    products = db.relationship('Product', backref=db.backref('entity'))

I've then used the inbuild create_api from flask-restless to generate api endpoints as follows:

manager.create_api(models.Entity,
include_columns = ['id'],
collection_name = 'entities',
methods=['GET', 'POST', 'PUT', 'DELETE'],
preprocessors={ etc..


manager.create_api(models.Product,
url_prefix = '/api/v1',
include_columns = ['imei'],
collection_name = 'products',
methods=['GET', 'POST','PUT', 'DELETE'],
preprocessors= { etc...

I've purposefully only included the primary key of both products and entities on the response.

If I do GET hostname/api/v1/products I get a list of just imei's. If I do GET hostname/api/v1/entities I get a list of just id's.

If I do `GET hostname/api/v1/products/{product_id}/entity it returns ALL information from the entity that owns the product {product_id}. This includes returning the information for EVERY product owned by the entity that owns product {product_id}.

How can I ensure that my include_columns or exclude_columns are still observed when using end-points that are "nested" in this fashion? The support for inclusion of specific attributes on relations, ie:

`include_columns = [Entity.id]' etc... works well for requests to the "base" URL, ie: hostname/api/v1/products, but not for "nested" urls ie: hostname/api/v1/products/1/enities

Any suggestions?

David McLean
  • 191
  • 1
  • 12

0 Answers0