hiroaki-yamamoto/mongoengine-goodjson

Pushing an Id to db.ListField(gj.FollowReferenceField()) adds an entire object

Opened this issue · 3 comments

Describe the bug
This is my controller:

user = User.objects.get(id=user_id)
company = Company(**body)
company.save()
user.update(push__companies=company.id)

I create a company and push its ID to user companies array.
This is the User Model:

email = db.StringField(required=True, unique=True)
password = db.StringField(required=True, exclude_to_json=True)
auth0 = db.StringField(unique=True)
name = db.StringField()
firstName = db.StringField()
lastName = db.StringField()
isAdmin = db.BooleanField(default=False)

companies = db.ListField(gj.FollowReferenceField(Company, reverse_delete_rule=PULL))

lastLogin = db.DateTimeField()
createdAt = db.DateTimeField()
updatedAt = db.DateTimeField(default=datetime.datetime.now)

So, sometimes when I insert a Company, it pushes the entire object to the user.companies.

To Reproduce
Steps to reproduce the behavior:
It is not deterministic. Sometimes it happens.

Expected behavior
I expect that it push only the ID, as a ReferenceField.

Can I use follow_reference=True on User.objects() query? Like:
Company.objects().to_json(follow_reference=True )

I couldn't do it. Is there any other way to do that?

@hubertokf Im in the same situation... you were able to solve the problem? Thanks

@yunaycompany I don't have this code anymore. Now I use postgres with django and I love it!

If I remember, to solve the serializer problem I had to override to_json method. Then I could build the serialized output the way I want.

I don't remember to have solved the ID in a referenceField.
Sorry.