我在我身上使用的是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