pyeve / eve-sqlalchemy

SQLAlchemy data layer for Eve-powered RESTful APIs
http://eve-sqlalchemy.readthedocs.io
Other
232 stars 70 forks source link

Return calculated (none ORM mapped) property for a resource #106

Closed vsobolmaven closed 7 years ago

vsobolmaven commented 8 years ago

Does eve-sqlalchemy allow to define an attribute on a resource class and return it in a result for GET request if the attribute is not mapped to ORM? e.g.

class ClassX(Base):
  @property
  def my_property(self)
     return "abracadabra"

registerSchema('res')(ClassX)

schema['schema']['my_property'] = {
  'type': 'string'
}

I need the property value as part of response with resource. Is this supported? Thanks!

mandarvaze commented 8 years ago

I am able to return additional attribute that is not mapped to ORM But I am not using the style you suggested above. Instead I am using on_fetched hook to append additional attributes to the response.

dkellner commented 7 years ago

Instead of using the on_fetched hook you can add the column to the projection, too.

mandarvaze commented 7 years ago

@dkellner Can you give an example how projection could be used to "insert" additional data that is not part of the DB ?

dkellner commented 7 years ago

Extending trivial.py:

class People(Base):
    # ...
    @property
    def foo(self):
        return "bar"
SETTINGS['DOMAIN']['people']['datasource']['projection']['foo'] = 1

If you want to add fields that are not part of the People class, you're out of luck with projections. In this case you have to resort to the event system.