mongoengine

InvalidDocument: Cannot encode object: <User: User object> ReferenceField with MongoEngine

怎甘沉沦 提交于 2019-12-12 03:59:56
问题 I've been working with Flask and MongoEngine, and I am having trouble when trying to save an object because of a ReferenceField. This is what my model looks like: class User(UserMixin, db.Document): first_name = db.StringField(max_length=255, required=True) last_name = db.StringField(max_length=255, required=True) email = db.StringField(max_length=255) class Post(db.Document): description = db.StringField(max_length=255, required=True) inserted_at = db.DateTimeField(default=datetime.datetime

Django Mongoengine unable to connect to MongoDB

淺唱寂寞╮ 提交于 2019-12-12 02:58:05
问题 We have build our environment using Django 1.7 , Mongoengine 0.8.7 , MongoDB 2.4.7 and Python 3.4 . We have installed django-rest-framework , but when we try to run the example of the website, we get the following error: ImproperlyConfigured at /users/ settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. Request Method: GET Request URL: http://localhost:8000/users/ Django Version: 1.7 Exception Type: ImproperlyConfigured

Override attribute access precedence having a data descriptor

天大地大妈咪最大 提交于 2019-12-12 01:24:34
问题 I have a bunch of instances of a MongoEngine model. And the profiler shows that a lot of time is spent in __get__ method of MongoEngine model fields: ncalls tottime percall cumtime percall filename:lineno(function) 1 0.066 0.066 26.525 26.525 entity.py:60(list) 2198 0.277 0.000 25.260 0.011 ***.py:96(***) 45603 0.822 0.000 24.832 0.001 ***.py:105(***) 285055 2.732 0.000 21.417 0.000 fields.py:189(__get__) 444491 2.643 0.000 17.476 0.000 dereference.py:12(__call__) As these model instances are

Pulling basic mongoengine document definitions into flask-mongoengine

江枫思渺然 提交于 2019-12-11 22:45:10
问题 I've been using mongoengine for a while now and have a ton of python data processing code that relies on a common set of Object Document Models. Now I need to access the same mongodb instances from Flask. I'd like to use the same ODM definitions. class User(Document): email = StringField(required=True) first_name = StringField(max_length=50) last_name = StringField(max_length=50) The problem is that flask-mongoengine requires you to first set up your flask context "db" and then build your ODM

Error using mod_wsgi, Django and MongoEngine

╄→гoц情女王★ 提交于 2019-12-11 14:46:59
问题 I have set up a Django site with MongoEngine and I am trying to run it using Apache and mod_wsgi with a virtualenv. It runs fine in development, but under Apache (on ubuntu 14.04.2) I get the following errors in the apache log: mod_wsgi (pid=16130): Exception occurred processing WSGI script '/data/.../.../wsgi.py'. Traceback (most recent call last): File "/data/.../.../wsgi.py", line 22, in application return get_wsgi_application()(environ, start_response) File "/data/venv/lib/python3.4/site

How do I tell if I inserted during upsert in mongoengine without (the deprecated) get_or_create?

蓝咒 提交于 2019-12-11 13:06:36
问题 I need to know if an upsert inserted, using MongoEngine (or if necessary, pymongo). The code looks like this: ret = MyMongoCollection.objects(name=desiredname) .update_one( { upsert: True, field1: 2 } ) Mongoengine seems to return only "num_affected" which is always exactly 1 here, by definition. I know these exist, but I'm looking for the Python flavor. How to do "insert if not exist else update" with mongoengine? Check if MongoDB upsert did an insert or an update 回答1: Here's another answer

F() objects in mongoengine similar to that in django

人走茶凉 提交于 2019-12-11 11:44:20
问题 Do we have something similar to F() objects in mongoengine, similar to that in Django. If yes then please help me in how to use it. If No then please suggest a way to incorporate similar functionality in mongoengine. Any help would be really appreciated. Thanks in advance. 回答1: You can use where operator ( since v0.5 ): User.objects( ).where( "this.firstname == this.lastname" ) Note that this is inefficient, since Mongo will not use indexes with these queries and thus it should be avoided:

MongoDB ReplicaSet Authentication Issue with Secondary

北战南征 提交于 2019-12-11 07:03:13
问题 We have a MongoDB (v 3.2.8) replicaSet with the following configuration: replication: replSetName: replica security: keyFile: mongo.key Our replicaSet status rs.status() currently looks like this: { "set" : "replica", "date" : ISODate("2016-11-11T15:43:29.164Z"), "myState" : 1, ... "members" : [ { "_id" : 4, "name" : "mongo_1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 155, ... "self" : true } { "_id" : 5, "name" : "mongo_2:27017", "health" : 1, "state" : 1,

How to sort a collection using the last element of an array

自闭症网瘾萝莉.ら 提交于 2019-12-11 06:28:43
问题 My problem is that, i have a collection below, _id is ignored { "value" : -10, "r" : [ { "v" : 1 }, { "v" : 3 } ] } { "value" : 2, "r" : [ { "v" : 4 }, { "v" : 1 } ] } { "value" : -100, "r" : [ { "v" : 4 }, { "v" : 1 }, { "v" : 10 } ] } { "value" : -3, "r" : [ ] } And i what to sort it by the last value of array r, that is to say, i want to have a result below, { "value" : -3, "r" : [ ] } # this one should be either the first one or the last one { "value" : 2, "r" : [ { "v" : 4 }, { "v" : 1 }

mongoengine default value from another field

谁都会走 提交于 2019-12-11 04:15:48
问题 I'm trying out MongoEngine for a project and its quite good. I was wondering if it is possible to set a default value for a field from another field? Something like this import mongoengine as me class Company(me.Document): short_name = me.StringField(required=True) full_name = me.StringField(required=True, default=short_name) this fails with an error ValidationError (Company:None) (StringField only accepts string values: ['full_name']) :EDIT: I did not mention that my app has a service layer