fastapi-filter icon indicating copy to clipboard operation
fastapi-filter copied to clipboard

Ignore missing fields when ordering in DynamicDocument

Open gehal4 opened this issue 2 years ago • 6 comments

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.

gehal4 avatar Jul 24 '23 16:07 gehal4

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.

github-actions[bot] avatar Jan 12 '24 01:01 github-actions[bot]

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.

arthurio avatar Jan 12 '24 16:01 arthurio

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.

github-actions[bot] avatar Apr 09 '24 01:04 github-actions[bot]