开发者

Inserts with Mongoengine work only from shell, but not from Django view

开发者 https://www.devze.com 2023-03-20 01:42 出处:网络
I am having strange situation - creating some Mongoengine object from Django shell is successful, but creating the same object from Django view looks like successful but without any data appeared in M

I am having strange situation - creating some Mongoengine object from Django shell is successful, but creating the same object from Django view looks like successful but without any data appeared in MongoDB. I.e. the same code like that -

from myapp.mongomodels import MyModel

m = MyModel(a=1, b=2, c=3)
m.save()

produces new object inserted into Mo开发者_Python百科ngoDB when running from manage.py shell, and produces nothing when running from Django view. I have traced the code and I am seeing mongoengine.Document.save() method is running correctly without any exceptions.

Looks like I've missed something obvious.

Will be grateful for any help.


You should be able to do that but force the save using:

from myapp.mongomodels import MyModel

m = MyModel(a=1, b=2, c=3)
m.save(force_insert=True)


As I've told before, the problem was in that I am assigning a primary key value BEFORE saving.

I have a system with some data stored in MySQL and some corresponding data stored in MongoDB. MySQL record is created first, related MongoDB record is created right after MySQL record, with the same primary key value. Thus, pymongo thinks thas this is update request instead of insert request, and no record is inserted at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消