mongolab沙箱数据库中的条目在django中没有save()

时间:2014-12-07 17:20:15

标签: django mongodb mlab django-mongodb-engine

我拥有的是带有Mongo引擎的Django设置。我使用了heroku的mongolab沙盒插件。通过heroku提供的mongolab设置在本地运行时,我观察到如果我创建了我的模型的实例,它会被保存到mongolab。它甚至不要求save()函数。

我有以下安装:

pip install git+https://github.com/django-nonrel/django@nonrel-1.5
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

在我的python shell(python manage.py shell)上:

>>>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

1 个答案:

答案 0 :(得分:0)

所以我忘了create()方法不只是插入而且还保存了条目。无需明确提供save()。