Search code examples
djangomongodbmlabdjango-mongodb-engine

Entry in mongolab sandbox database happens without save() in django


What i have with me is a setup of Django with Mongo engine. I have used mongolab sandbox addons from heroku. While running locally with mongolab settings provided via heroku, what i observe that if i create an instance of my model, it gets saved to mongolab. It doesnot even ask for save() function.

I have the following installations:

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

I also have pymongo installed

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

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

this gets saved to the database specified on settings.py at this very step itself.

from documentation:

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

Am i doing something wrong? This is my first time with Mongodb


Solution

  • So i forgot that create() method not just inserts but also saves the entry. There is no need for providing save() explicitly.