Replies: 8 comments
-
|
As far as I know there's nothing in fastAPI that requires SQLAlchemy, i myself use asyncpg Barring that I would imagine you need to use their json utilility to get what you want, python doesn't know how to encode ObjectId which is an object returned by the mongo response https://api.mongodb.com/python/current/api/bson/json_util.html Personally I'd write a Custom Response class that encapsulates this. Something like MongoJSONResponse an example of one that already exists and usage can be found here. https://fastapi.tiangolo.com/advanced/custom-response/#orjsonresponse |
Beta Was this translation helpful? Give feedback.
-
|
Small example: from fastapi import FastAPI
from mongoengine import *
connect('FastAPILearn')
app = FastAPI()
class HumanDB(Document):
name = StringField(max_length=120)
age = IntField(min_value=0)
@app.get("/users")
def get_users():
return {'data': HumanDB.objects.all()}
@app.get("/user")
def get_user():
return {'data': HumanDB.objects.first()}First method cause |
Beta Was this translation helpful? Give feedback.
-
|
I found some workaround, but it seems to be very dirty. class HumanDB(Document):
name = StringField(max_length=120)
age = IntField(min_value=0)
def to_dict(self):
return {'id': str(self.id),
'name': self.name,
'age': self.age}
@app.get("/users")
def get_users():
return {'data': list(map(lambda h: h.to_dict(), HumanDB.objects))}
@app.get("/user")
def get_user():
return {'data': HumanDB.objects.first().to_dict()} |
Beta Was this translation helpful? Give feedback.
-
|
Have you tried using the json utils described above https://api.mongodb.com/python/current/api/bson/json_util.html I truly think that's the solution to your problem. You need a serializer that understands mongo objects, that serializer does. |
Beta Was this translation helpful? Give feedback.
-
|
You are getting this error
because Mongo's _id field is a bytes object. you can circumvent this by suppressing the _id field when you return it.
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here everyone! 👏 🙇 I assume you solved your problem, so thanks for reporting back and closing the issue @KirillSaltykov 👍 |
Beta Was this translation helpful? Give feedback.
-
My issue now is I need to return the _id field and I end up getting the same error. |
Beta Was this translation helpful? Give feedback.
-
|
@tuxnani if you are still having problems, please create a new issue following the template, providing a minimal, self-contained example, etc. Also, check out ODMantic: https://art049.github.io/odmantic/
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First check
Description
How can I use MongoDB using libraries such a pymongo or MongoEngine?
I tried to create a basic project with pymongo, but when I try to return MongoDB entity using
.find_one()for example, I got an error that... ObjectId does not iterable....Then I tried MongoEngine, but when I return for example
Article.objects, it just returns"_cls": "".Is it possible to use MongoDB in FastAPI or I can use only SQLAlchemy?
Additional context
I also found similar issues in a repo and some answers on StackOverflow but doesn't found how to return MongoDB entities in fastapi responses.
Beta Was this translation helpful? Give feedback.
All reactions