Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

No ID replace when returning list of objects #34

Closed
shlomiLan opened this issue Jul 21, 2018 · 4 comments
Closed

No ID replace when returning list of objects #34

shlomiLan opened this issue Jul 21, 2018 · 4 comments
Assignees

Comments

@shlomiLan
Copy link

When I run this:

instance = self.api_class.objects.get_or_404(id=object_id)
return instance.to_json()

I get a single document with the ID field formated correctly:

{"amount": 1, "descreption": "BBB", "pay_method": "5b51fed52c88845720509757", "id": "5b5101de2c88845fdf1429ed"}

But when I do:

return self.api_class.objects.to_json()

I get the following list without the IDs been formatted:

[{"_id": {"$oid": "5b5101de2c88845fdf1429ed"}, "amount": 1, "descreption": "BBB", "pay_method": {"$oid": "5b51fed52c88845720509757"}}, {"_id": {"$oid": "5b5204772c88845720509759"}, "amount": 100, "descreption": "BBB", "pay_method": {"$oid": "5b51fed52c88845720509757"}}]

Am I doing anything wrong?

@hiroaki-yamamoto hiroaki-yamamoto self-assigned this Jul 24, 2018
@hiroaki-yamamoto
Copy link
Owner

Well, simple encode/decode is already tested by test code (Check queryset integration test), but I think this issue might happens when you code a complex code.

Did you inherit/override something other than mongoengine_goodjson.document.Document?

@shlomiLan
Copy link
Author

I don't understand your solution.
My class is quite simple but I also used this package.
Now I have solved it by implementing the to_json method by myself in a class and then all my document Inheriting from it.

@hiroaki-yamamoto
Copy link
Owner

hiroaki-yamamoto commented Aug 4, 2018

Hi,
As far as I read the code of that, flask_mongoengine seems to have their own Document class and QuerySet class that don't inherit mongoengine_goodjson.Document and mongoengine_goodjson.QuerySet. As the solution, you will need to create your own classes that inherit the both of mongoengine_goodjson classes and flask_mongoengine classes.

@shlomiLan
Copy link
Author

That solved my problem, but I had to set my class as follows:

class Expense(db.Document, gj.Document)

the other way (class Expense(gj.Document, db.Document)), didn't work

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants