我在我身上使用的是Django与Mongo引擎的设置。我使用了heroku的mongolab沙箱插件。当通过heroku提供的mongolab设置在本地运行时,我观察到如果我创建了模型的实例,它将保存到mongolab。它甚至不要求save()函数。在MongoDB沙箱数据库中输入没有保存()在Django中
我有以下设施:
pip install git+https://github.com/django-nonrel/[email protected]
pip install git+https://github.com/django-nonrel/djangotoolbox
pip install git+https://github.com/django-nonrel/mongodb-engine
我也有pymongo安装
settings.py:
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django_mongodb_engine'
DATABASES['default']['NAME'] = 'database'
DATABASES['default']['USER'] = 'username'
DATABASES['default']['PASSWORD'] = 'password'
DATABASES['default']['HOST'] = 'host'
DATABASES['default']['PORT'] = 'port'
modles.py:
from django.db import models
from djangotoolbox.fields import ListField
class Post(models.Model):
title = models.CharField(max_length=20)
text = models.TextField()
tags = ListField()
comments = ListField()
def __unicode__(self):
return self.title
在我蟒蛇壳(蟒蛇管理e.py外壳):
>>>from myapp.models import Post
>>>a = Post.objects.create(title="abc", text="pqr", tags=["wer","tyu"], comments=["ret","swe"])
>>>a
<Post: abc>
这将被保存到settings.py中指定的数据库,在这一步本身。
从文档:
MongoEngine tracks changes to documents to provide efficient saving. To save the document to the database, call the save() method. If the document does not exist in the database, it will be created. If it does already exist, then any changes will be updated atomically
我做错什么了吗?这是我第一次与Mongodb