简体   繁体   English

使用from_json制作的MongoEngine文档对象不保存

[英]MongoEngine Document Object made using from_json doesn't save

I am trying to build a document object using from_json method. 我正在尝试使用from_json方法构建一个文档对象。 object.save() throws no error, but the document is not inserted in mongo. object.save()不会抛出任何错误,但文档未插入mongo。

On the other hand if I make the object by assigning values to each of the fields, it works fine. 另一方面,如果我通过为每个字段赋值来创建对象,它可以正常工作。

I am unable to find the reason for this. 我无法找到原因。 Below is the code for both the cases. 以下是两种情况的代码。

from flask import Flask
from flask.ext.mongoengine import MongoEngine
import json, datetime

app = Flask(__name__)
app.config["MONGODB_SETTINGS"] = {'DB': 'test','host': 'localhost'}
app.config["SECRET_KEY"] = "mySecretKey"

db = MongoEngine(app)
class User(db.Document):
    user_id = db.StringField(max_length=16, primary_key = True)
    username = db.StringField(min_length=8)
    email = db.EmailField(required = True, unique = True)
    password = db.StringField(required = True)
    date_of_birth = db.DateTimeField()
    gender = db.StringField(choices = ('M', 'F'))

'''
    This one works. This will add a user in local mongodb(test)
'''
u1 = User()
u1.username = 'test12345'
u1.user_id = 'testid12345'
u1.email = 'test@test.com'
u1.password = 'testerpass'
u1.save()

'''
    This one doesn't works.
'''
u2 = User()
temp_json = {'username':'test2_12345','user_id':'testid2@12345','password':'testerpass2','email':'test2@test.com'}
u2 = u2.from_json(json.dumps(temp_json))
u2.save()

A mongoengine document object can be initialised with **kwargs . 可以使用**kwargs初始化mongoengine文档对象。 Thus using this we can implement the from_json functionality in the following way :- 因此,我们可以通过以下方式实现from_json功能: -

obj_dict = {
    'key1' : 'value1',
    'key2' : 'value2'
}
user = User(**obj_dict) # User is a mongoengine document object

This worked for me. 这对我有用。

from_json() is convert to JSON data an unsaved document instance. from_json()未保存的文档实例转换为JSON数据。 Do save of unsaved document is use parameter force_insert=True . 保存未保存的文件是使用参数force_insert=True

...
>>> User.objects
[<User: test1-12345>]
>>> u2.save()
>>> User.objects
[<User: test1-12345>]
>>> u2.save(force_insert=True)
>>> User.objects
[<User: test1-12345>, <User: test2-12345>]

But, your code I can. 但是,我的代码可以。

I can not code here. 我不能在这里编码。 (I need fixture data of unit test.) (我需要单元测试的夹具数据。)

I environment Django 1.6.5 and mongoengine 0.8.7 我环境Django 1.6.5和mongoengine 0.8.7

>>> json_data1 = u1.to_json()
>>> User.objects.delete() # or User.drop_collection()
>>> User.objects
[]
>>>
...
# json_data1 to dump for pickle. Next load for pickle.
...
>>> u1 = User.from_json(json_data1)
>>> u1.save()
>>> User.objects
[]
>>> u1.save(force_insert=True)
>>> User.objects
[<User: test1-12345>]
>>>

force_insert=True is only try to create a new document. force_insert=True只是尝试创建一个新文档。

Every time use force_insert=True is create a new document. 每次使用force_insert=True都会创建一个新文档。

Use force_insert=False is get document in database. 使用force_insert=False是在数据库中获取文件。

You are assigning u2 to the result of from_json() , and losing reference to the original User object. 您将u2分配给from_json()的结果,并丢失对原始User对象的引用。

Change u2 = u2.from_json(... to u2.from_json(... 改变u2 = u2.from_json(...u2.from_json(...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM