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
但是如何在端点模型上执行自定义查询以及如何传递除“id,limit,order ..”之外的自定义参数等
For eg :
例如:
return query.filter(Student.name == somename )
How can I supply "somename" to the endpoint ?
如何向端点提供“somename”?
1 个解决方案
#1
3
If the property you want to filter by is part of your Model you can just add 'name' as query_field
如果要过滤的属性是模型的一部分,则可以将“name”添加为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.
如果API请求中提供了name,则会自动应用相等过滤器(MyModel.name == name)。
If you need more custom queries you can work with EndpointsAliasProperty
on your model and directly access the model's _endpoints_query_info._filters
如果您需要更多自定义查询,可以在模型上使用EndpointsAliasProperty并直接访问模型的_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.
如果API请求中提供了minDate,这将自动应用MyModel.updated> = minDate过滤器。
#1
3
If the property you want to filter by is part of your Model you can just add 'name' as query_field
如果要过滤的属性是模型的一部分,则可以将“name”添加为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.
如果API请求中提供了name,则会自动应用相等过滤器(MyModel.name == name)。
If you need more custom queries you can work with EndpointsAliasProperty
on your model and directly access the model's _endpoints_query_info._filters
如果您需要更多自定义查询,可以在模型上使用EndpointsAliasProperty并直接访问模型的_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.
如果API请求中提供了minDate,这将自动应用MyModel.updated> = minDate过滤器。