Git Product home page Git Product logo

Comments (19)

macfire avatar macfire commented on June 12, 2024 1

@hiroaki-yamamoto I discovered my error.

I did not append .get() method when retrieving objects

#only returns referenced document object ids
user = User.objects(id=ObjectId("57e968198d84c00035c82c63"))
return jsonify(json.loads(user.to_json()))
#returns referenced complete document as embedded document
user = User.objects(id=ObjectId("57e968198d84c00035c82c63")).get()
return jsonify(json.loads(user.to_json()))

from mongoengine-goodjson.

macfire avatar macfire commented on June 12, 2024 1

This feature is very helpful. Thanks.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024 1

Perhaps, I think your document schema (i.e. class that inherits Document or EmbeddedDocument) inherits mongoengine.Document and mongoengine.EmbeddedDocument and they disallow follow_reference kwargs.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024 1

Done, but there's a missing line on coverage.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Hi @macfire,

Do you mind if you tell me what db and gj are?

from mongoengine-goodjson.

macfire avatar macfire commented on June 12, 2024

I used factory pattern to init flask_mongoengine (potential problem?)

#extensions.py:
from flask_mongoengine import MongoEngine
db = MongoEngine()

#myapp.py
db.init_app(app)
#models.py

from myapp.extensions import db
import mongoengine_goodjson as gj

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Ok,

How about using gj.FollowReferenceField, not db.FollowReferenceField?

from mongoengine-goodjson.

macfire avatar macfire commented on June 12, 2024

Sorry for the confusion, I miss-wrote the example above.
My actual code is using gj.FollowReferenceField
(I've changed the example above to reflect this)

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Oh... ok

However, this smells like a bug because I expected FollowReference should work on QuerySet as well as Document.

from mongoengine-goodjson.

macfire avatar macfire commented on June 12, 2024

@hiroaki-yamamoto You may close this issue, unless you want to leave it open in regards to the bug.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Please leave as-is, because it needs investigation. If you don't want "many messages", plese unsubscribe.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Note

  • queryset.py line 22 doesn't seem to support following reference and as_pymongo returns "dicts" in a list. And filtering the fields that is typed as FollowReferenceField and ReferenceField.
  • For from_json, perhaps it seems to be ok because queryset.py line 56 uses _from_son, but needs tests because it is unclear.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Branch: 15_queryset_follow_reference

from mongoengine-goodjson.

leerobert avatar leerobert commented on June 12, 2024

I really love this library as it makes my model class way less complex. thanks so much for your work...

I currently get a similar error when attempting to follow reference

>>> d = Drawing.objects(id='57ed40b59d1fa2ab84ea8751').get()
>>> d.to_json()
'{"strokes": [{"drawn_at": "2016-09-29T16:26:27",....
>> d.to_json(follow_reference=True)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/robert/Projects/api-v2/env/lib/python3.5/site-packages/mongoengine_goodjson/document.py", line 183, in to_json
    max_depth, current_depth, use_db_field, *args, **kwargs
  File "/Users/robert/Projects/api-v2/env/lib/python3.5/site-packages/mongoengine_goodjson/document.py", line 66, in _follow_reference
    ) if doc else doc
  File "/Users/robert/Projects/api-v2/env/lib/python3.5/site-packages/mongoengine/base/document.py", line 422, in to_json
    return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
  File "/Users/robert/Projects/api-v2/env/lib/python3.5/site-packages/bson/json_util.py", line 113, in dumps
    return json.dumps(_json_convert(obj), *args, **kwargs)
  File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/Users/robert/Projects/api-v2/env/lib/python3.5/site-packages/mongoengine_goodjson/encoder.py", line 40, in __init__
    super(GoodJSONEncoder, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'current_depth'

This is on the latest branch on pypi (0.11.3).

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

@leerobert Could you give me the schema?

from mongoengine-goodjson.

leerobert avatar leerobert commented on June 12, 2024

You're correct I am using mongoengine. I used the FollowReferenceField and had no problems with that. Unfortunately I do have some circular references so I won't be able to use that field for all of my models.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Okay, anyway I will write test cases.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024
{
    u"id": text_type(reference.id),
    u"name": u"test",
    u"references": [article_dict.copy()],
    u"ex_info": {
        u"txt": reference_extra_info.txt
    },
    u"ex_ref": {
        u"_id": {u"$oid": str(reference_extra_ref.id)},
        u"ref_txt": reference_extra_ref.ref_txt
    }
}

Note: I think this _id should be id and the id should be str, not dict.

from mongoengine-goodjson.

hiroaki-yamamoto avatar hiroaki-yamamoto commented on June 12, 2024

Closed

from mongoengine-goodjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.