0

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?

James
  • 2,488
  • 2
  • 28
  • 45

1 Answers1

2

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')
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • The verbiage of the docs and perhaps my ignorance has led me to great pain. :) I thought .create() _was_ how you instantiate an object! But now I understand the difference based on what you wrote and it's exactly what I wanted to do. Thank you! – James Feb 11 '14 at 00:03
  • In general, methods on the manager class (`objects`) work with the database. – Peter DeGlopper Feb 11 '14 at 00:07