Search code examples
pythondjangomongodbdjango-mongodb-engine

Django Prevent Automatic Save During Object.Create


First off disclaimer: I'm using django-mongodb-engine it's possible the issue I'm observing is due to the different db driver. In any event, it seems that calling MyModel.object.create() actually creates a database entry. This is contrary to the django documentation that states "Note that instantiating a model in no way touches your database; for that, you need to save()." Source. Here is an example:

In [4]: MyModel.objects.filter(email='test')
Out[4]: [<MyModel: MyModel object>]

In [5]: MyModel.objects.create(email='test')
Out[5]: <MyModel: MyModel object>

In [6]: MyModel.objects.filter(email='test')
Out[6]: [<MyModel: MyModel object>, <MyModel: MyModel object>]

As you see above, create() did indeed "touch the database." Is there any way to prevent this behavior?


Solution

  • That is the expected behavior. create creates a record in the db. The doc passage you quote is referring to instantiating the model in memory:

    my_instance = MyModel(email='test')