jkurtisr32 jkurtisr32 - 3 months ago 12
Python Question

Creating API responses with flask-restless

Let's say I have two models in SqlAlchemy:


  • UserType

  • User



User
has a foreign key to
UserType


I have properly configured flask-restless to serve these models as API endpoints:


  • /api/user

  • /api/user_type



But by default, when I visit either of these endpoints, I get the related data associated with each object in the response:


  • Each
    User
    has the corresponding
    UserType
    object nested in the response

  • Each
    UserType
    has a collection of
    User
    nested in the response



This will definitely lead to a lot of overhead as the data grows a lot. If I just want to GET the list of
UserType
that the system supports, all of the associated users will come back. Usually an API would generate a link for related resources:


  • /api/user/1/user_type

  • /api/user_type/1/users



Has anyone gotten these links out of flask-restless responses?

Answer

It looks like this is a known issue that has been active on GitHub for a long time. Not sure if the developer is planning on fixing it or not:

https://github.com/jfinkels/flask-restless/issues/168#issuecomment-69821642

I ended up going with flask-restful and building my own endpoints.