mongoengine-goodjson icon indicating copy to clipboard operation
mongoengine-goodjson copied to clipboard

got an unexpected keyword argument 'follow_reference'

Open rlaurente opened this issue 4 years ago • 1 comments

Describe the bug When I tried this, it gets that follow_reference error

products = Product.objects() result = json.loads(products.to_json(follow_reference=True))

but if I add .first() or .get() it works. products = Product.objects().first() result = json.loads(products.to_json(follow_reference=True))

Additional context

from mongoengine import * import mongoengine_goodjson as gj

class Address(gj.EmbeddedDocument): street = StringField() street2 = StringField() city = StringField state = StringField() zip_code = StringField() country = StringField() borough = StringField() area = StringField() county = StringField() latitude = IntField() longitude = IntField()

class Store(gj.Document): name = StringField() description = StringField() domain = StringField()

class ProductPrice(gj.EmbeddedDocument): store = gj.FollowReferenceField(Store) price = FloatField()

class Product(gj.Document): name = StringField() picture = URLField() upc = URLField() description = StringField() prices = ListField(EmbeddedDocumentField(ProductPrice)) tags = ListField(StringField(choices=['SALE', 'POPULAR']))

rlaurente avatar Jun 19 '20 20:06 rlaurente

👍 Here; It seems that the problem is that follow_reference is not implemented when the result set has several records

This is inconsistent with mongoengine to_json method that works on all results sets;

See this code below

print("one supplier : " +  Supplier.objects.first().to_json())
print("all suppliers : " + Supplier.objects.all().to_json())

Returns

one supplier : {"id": "61090c626bb675ca4e3f844c", "name": "Nory Sup", "created": "2021-08-03T09:29:06.535254"}
all suppliers : [{"name": "Nory Sup", "id": "61090c626bb675ca4e3f844c"}, {"name": "new one1", "created": "2021-08-03T09:29:06.535000", "modified": "2021-08-03T09:29:06.593000", "id": "61090c62cf371f3947f64d3b"}]

With follow reference, this works

print("one supplier follow : " + Supplier.objects.first().to_json(follow_reference=True))
one supplier follow : {"id": "61090c626bb675ca4e3f844c", "name": "Nory Sup", "created": "2021-08-03T09:29:06.535254"}

But the results set response called below

print("all suppliers follow : " + Supplier.objects.all().to_json(follow_reference=True)) 

Fails with the following error

File "/Users/offer-nory/devel/projects/nory/Inventory-Service/src/scripts/playground.py", line 35, in do_stuff
    print("all suppliers follow : " + Supplier.objects.all().to_json(follow_reference=True))
  File "/Users/offer-nory/.virtualenvs/Inventory-Service/lib/python3.9/site-packages/mongoengine_goodjson/queryset.py", line 98, in to_json
    return json.dumps(lst, *args, **kwargs)
  File "/opt/homebrew/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 234, in dumps
    return cls(
  File "/Users/offer-nory/.virtualenvs/Inventory-Service/lib/python3.9/site-packages/mongoengine_goodjson/encoder.py", line 38, in __init__
    super(GoodJSONEncoder, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'follow_reference'

Will be good to get a response on this please !

osnory avatar Aug 03 '21 09:08 osnory