Ignore missing fields when ordering in DynamicDocument
When the order_by is set in request with a field that is not registered in the model, I get this error message :
{
"detail": [
{
"loc": [
"order_by"
],
"msg": "myfield is not a valid ordering field.",
"type": "value_error"
}
]
}
My model, which doesn't contain the field : myfield, looks like:
class MyModel(DynamicDocument):
somefield = fields.StringField()
# myfield is missing, which is normal in case of using DynamicDocument
meta = {'collection': 'mycollection'}
The Filter :
class MyModelFilter(Filter):
somefield : Optional[str]
myfield : Optional[str]
search: Optional[str]
order_by: list[str] = ["myfield"]
class Constants(Filter.Constants):
model = MyModel
Is there any work around for this to ignore, or can we dev this to configure in Constants class somehow?
Also, there is no issue for filtering myfield, event if not present in the model, error message is returned only for ordering.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 5 days.
Hi @gehal4, first of all sorry for the (very) delayed reply.
This is issue is due to the check here: https://github.com/arthurio/fastapi-filter/blob/main/fastapi_filter/base/filter.py#L94
We might want to have a different implementation for mongo since it's more dynamic as you mentioned 🤔 .
In the meantime, you should be able to override validate_order_by to skip that validation and your order_by should work.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 5 days.