identixone / fastapi_contrib

Opinionated set of utilities on top of FastAPI

Home Page:https://fastapi-contrib.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing extra arguments to MongoDBModel.list()

dasv opened this issue · comments

commented
  • FastAPI Contrib version: 0.1.14
  • FastAPI version: 0.42
  • Python version: 3.7.4
  • Operating System: Debian 10

Description

Hi,
I am trying to get a list of entries from a collection sorted in descending order according to a recorded date (not a creation timestamp, I am not using the timestamped model). Pymongo sort should work passing a sort=[(key, value)] argument, but when I do that, I get back an empty list. Am I doing anything wrong?

What I Did

This is my function. The field transmit_time is a MongoDB date field.

@router.get("/")
async def get_packets(limit: int = 100, offset: int = 0):
    sort_desc = [('transmit_time', -1)]
    packets = await Packet.list(_limit=limit, _offset=offset, sort=sort_desc)
    return packets

Hi, @dasv

That is because internally we pass to list method kwargs as dict to pymongo as a first argument (which is sort=sort_desc in your example). So when we call pymongo's list method to get the cursor we pass it {"sort": [('transmit_time', -1)]} which is not correct filter, neither correct ordering. We don't do any stripping based on the names of keyword arguments passed to list.

To solve this problem, we released 0.1.15 version which adds new keyword argument _sort to thelist method of mongodb models. In this version we also separated the optional packages from the main dependency, which is FastAPI. For example, to install this library with mongodb, ujson and pytz as it was before, you should do now: pip install fastapi_contrib[mongo,ujson,pytz]==0.1.15.

So, to make your example work, you can just upgrade the version and change your code to the following:

@router.get("/")
async def get_packets(limit: int = 100, offset: int = 0):
    sort_desc = [('transmit_time', -1)]
    packets = await Packet.list(_limit=limit, _offset=offset, _sort=sort_desc)
    return packets
commented

Fantastic! But sadly, it seems that I cannot install it because of the FastAPI version requirements: fastapi_contrib 0.1.14 is compatible with any version of FastAPI >= 0.35.0, but fastapi_contrib 0.1.15 is limited to fastapi <=0.37.0,>=0.35.0. I am using FastAPI 0.42, the latest version. What are the compatibility reasons for this change?

Seeing how the arguments are passed, passing **{"$orderby": {"transmit_time": -1}} would work, but the $orderby operator is deprecated for mongo > 3.2...

The reason for pinning version in such way is that multiple newer versions of FastAPI have different version of Pydantic, which this package heavily relies on, and on that newer version multiple tests are failed. In fact, various of the Pydantic versions (which have changed quite a few times, being a dependency of FastAPI from 0.35.0 to current 0.42.0, they've changed in minor versions and grew rapidly with the FastAPI itself) are breaking this package in different ways. Last time I checked, one of the related changes that broke our code is from FastAPI itself (so it has not so big impact as Pydantic did).

I think, now that Pydantic is actually 1.0, we can try and write some compatibility code for the current pinned versions of FastAPI and aim for support of the major version, as well as current latest FastAPI (this should not be a big problem).

I guess this is going to be 0.2 of this package after we get Pydantic versions sorted out.

There are long going plans for making it to the 1.0 as well and probably there are going to be separate packages of mongo-related stuff, opentracing stuff, and possibly even more. The goal is to figure out what the core should be and give it a clear vision.

commented

Oh, I see. For the time being, I can downgrade to 0.37 as I was not using many of the new features, just allowing "None" for path parameters, and being able to sort results is much more important.
You're doing great work! Thank you!

Hi @dasv , if you're still interested, I've just released 0.1.16 version of this package with support for the latest FastAPI (0.42.0).