1
votes

Normally we do queries like this

@MyModel.query_method(path='mymodels', name='mymodel.list')
def MyModelList(self, query):
   return query

But how can I perform custom queries on Endpoints model and how do I pass custom parameters other than "id, limit, order.." etc

For eg :

return query.filter(Student.name == somename )

How can I supply "somename" to the endpoint ?

1

1 Answers

3
votes

If the property you want to filter by is part of your Model you can just add 'name' as query_field

@MyModel.query_method(path='mymodels',
                      name='mymodel.list',
                      query_fields=('name',))

This will automatically apply an equality filter (MyModel.name == name) if name is supplied in the API request.

If you need more custom queries you can work with EndpointsAliasProperty on your model and directly access the model's _endpoints_query_info._filters

Example for doing an inequality filter with a date:

class MyModel(EndpointsModel):

    ...
    updated = EndpointsDateTimeProperty(auto_now=True)
    ...

    def MinDateSet(self, value):
        if value is not None:
            self._endpoints_query_info._filters.add(MyModel.updated >= value)

    @EndpointsAliasProperty(setter=MinDateSet,
                            property_type=message_types.DateTimeField)
    def minDate(self):
        """
        minDate is only used as parameter in query_methods
        so there should never be a reason to actually retrieve the value
        """
        return None


@MyModel.query_method(path='mymodels',
                      name='mymodel.list',
                      query_fields=('minDate',))

This will automatically apply the MyModel.updated >= minDate filter if minDate is supplied in the API request.