mongoengine

Manytomany field in Django mongoengine Document

我的未来我决定 提交于 2019-12-10 22:58:27
问题 I have the following Django model: from mongoengine import * from datetime import datetime class Company(Document): name = StringField(max_length=500) class Feedback(Document): text = StringField(max_length=500) is_approved = BooleanField(default=False) date = DateTimeField(default=datetime.now()) I want to add a manytomany field of Feedback in Company Thanks in advance. 回答1: This is not a Django model, but a mongoengine Document . It does not have ManyToManyField . Instead you should

Mongoengine PointField gives location object expected, location array not in correct format error

試著忘記壹切 提交于 2019-12-10 19:12:30
问题 I have a model as follows: class Station(Document): location = PointField() Trying to write data as follows: station = Station() station.location = { "type": "Point", "coordinates": [ 81.4471435546875, 23.61432859499169 ] } station.save() However this gives the error Could not save document (location object expected, location array not in correct format) Mongoengine documentation says such a dictionary should be OK. What am I missing here? 回答1: I face similar problem once, in my case it

ListField is showing <ul> instead of <input> in edit/create post

隐身守侯 提交于 2019-12-10 18:54:59
问题 I am using Flask, mongoengine for a project and I am trying to get basic stuff working from http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-flask-mongoengine/ After implementing everything from above link I added a new field for "tags" in Post and when I try to create a post, my tags doesn't show a input box. Any help is appreciated. My code and screenshot below class Post(db.DynamicDocument): created_at = db.DateTimeField(default=datetime.datetime.now, required

AttributeError with Django REST Framework and MongoEngine

本小妞迷上赌 提交于 2019-12-10 09:25:49
问题 I am trying to use Django and the Django REST Framework together with MongoEngine but it doesn't seem to work for me. I don't know where things go wrong ... perhaps someone can help me out. Here is the code: models.py from mongoengine import * class Lady(Document): firstname = StringField() lastname = StringField() serializers.py from rest_framework import serializers from mongoengine import * class LadySerializer(serializers.Serializer): firstname = serializers.CharField(max_length=50)

MongoEngine文档翻译__新手教程(二)定义文档

安稳与你 提交于 2019-12-10 08:35:41
在MongoDB里面,一条文档大致相当于关系型数据库里面的一行。在关系型数据库里面,行是被存储在表里面,并且有一个严格的结构。MongoDB里面把文档存储在集合里面而不是存在表里面,最根本上的不同就是在数据库层面上没有强制的结构限制。 定义一个文档纲要 MongoEngine允许你为文档定义一个纲要这可以帮你减少编码错误,让你利用现有的字段来定义各种功能的函数。 定义一个文档的纲要,首先需要创建一个继承 Document 的类。文档的字段都被作为这个继承类的属性。 [python] from mongoengine import * import datetime class Page(Document): title = StringField(max_length= 200 , required= True ) date_modified = DateTimeField(default=datetime.datetime.now) 动态文档纲要 MongoDB的一个好处就是为集合定义动态的纲要,这在有动态文档的场景下能让数据有组织,有计划的存储。 动态文档与一般文档的工作方式一样,但是任何为它们设置的数据或属性也会被存储 [python] from mongoengine import * class Page(DynamicDocument): title =

MongoEngine__新手教程(五)在django框架里使用mongodb

折月煮酒 提交于 2019-12-10 08:20:14
先很简单的创建一个django的工程(具体不说django), 然后弄个小app或者随便哪里写个view就好了. 然后我用了几步就确定它可以正常使用了. 首先修改settings.py, 原来DATABASES完全不用去管它了, 全部设为空串就好, 然后在文件里加上下面的内容(这里的连接方式可以自行更换,比如换成url连接的,这里假设是使用本地的mongodb数据库,并且mongodb都是使用的默认配置) python 1 2 from mongoengine import connect connect ( 'DB_NAME' ) 在models.py里随便写个模型, 这里要用到mongoengine的一些内容 python 1 2 3 4 5 from mongoengine import Document class TestModel ( Document ): test_key = StringField ( required = True ) test_value = StringField ( max_length = 50 ) 在某个views.py里随便哪里写点逻辑, 添加条数据而已(两种方式都可以填数据) python 1 2 3 4 from app.models import TestModel entry = TestModel ( test_key =

MongoEngine _types and _cls fields

删除回忆录丶 提交于 2019-12-09 18:00:54
问题 Why does mongoengine add _types and _cls fields to every document of a collection. Both of them are a (key, value) pair and both of them contain the name of the document's model class. The only difference is _types value is a list and I assume it can have multiple model class names if there is involved some inheritance. However the question is: why do I need them to exist in every document within a collection when all the documents will have the same values for both fields? 回答1: Mongoengine

MongoEngine文档翻译__新手教程(一)安装MongoEngine&连接MongoDB

蓝咒 提交于 2019-12-09 13:11:54
PS:非常不错的mongoengine新手教程 最近开始做一个Python + MongoDB的项目,用到了MongoEngine这个非常不错的ORM工具,我将MongoEngine的文档翻译一部分出来,与大家分享。 安装MongoEngine 为了使用MongoEngine,我们首先需要先下载一个MongoDB并且确保它能正常运行,你还需要安装pymongo。 可以用pip安装MongoEngine: $ pip install mongoengine 但是如果你没有安装setuptool,那么下载一个 MongoEngine ,然后手动安装 $ python setup.py install 如果你想用最新得MongoEngine,可以从GitHub上下载源码,然后按如下安装: $ git clone git://github.com/hmarr/mongoengine $ cd mongoengine $ python setup.py install ps:由于mongoengine底层使用的是pymongo库,所以安装mongoengine的时候一定要安装与其版本配套的pymongo版本,否则在使用的时候或出现调用pymongo上的错误。(一般建议安装最新版的pymongo,否则要上网查下版本匹配)。 连接MongoDB 连接一个运行的MongoDB实例

OperationFailure: not authorized on tracking to execute command

◇◆丶佛笑我妖孽 提交于 2019-12-08 19:23:26
问题 I did the following -- `sudo apt-get install mongodb-org` -- go to `etc/mongod.conf` change bindIp to: `0.0.0.0` -- sudo mkdir /data/db -- start without auth to create user `sudo mongod --port 27017 --dbpath /data/db` -- open shell with : mongo --port 27017 ``` > use admin > db.createUser( { user: "useradmin", pwd: "mypassword", roles: [ { role: "root", db: "admin" } ] } ) ``` -- Restart with auth required(ctrl+c the above mongod process): `sudo mongod --auth --port 27017 --dbpath /data/db' -

sorting a ListField in mongoengine

ε祈祈猫儿з 提交于 2019-12-08 10:23:16
问题 I have a model in mongoengine defined like this: class Task(Document): name = StringField(required=True, unique=True) frequency = IntField(required=True) quantity = IntField() units = StringField() events = ListField(DateTimeField(default=datetime.datetime.now)) How can I get the latest event ? I've tried the following to no success: def latest(self): return self.events.sort()[-1] Instead of returning the events sorted sort returns None 回答1: You could just use the Mongoengine SortedListField