I have django 1.3.1, python2.6 and MySQL 5.5.20.
init_command in setting.py is set to SET storage_engine=INNODB.
I have the following situation:
Consider the model:
class Fruit(models.Model):
name = models.CharField(max_length=50)
def __unicode__(self):
return self.name
Now I open two Django shells and type:
django shell I:
./manage.py shell
(InteractiveConsole)
>>> import fruits.models as m
>>> m.Fruit(name="apple").save()
>>> m.Fruit.objects.get(pk=1)
<Fruit: apple>
django shell II:
./manage.py shell
(InteractiveConsole)
>>> import fruits.models as m
>>> m.Fruit.objects.get(pk=1)
<Fruit: apple>
So far so good. Now I continue to type in shell I:
>>> m.Fruit(name="peach").save()
>>> m.Fruit.objects.get(pk=2)
<Fruit: peach>
Qestion: Why typing the following in the shell II does not find the object?
>>> m.Fruit.objects.get(pk=2)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "..../django/db/models/manager.py", line 132, in get
return self.get_query_set().get(*args, **kwargs)
File "..../django/db/models/query.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: Fruit matching query does not exist.
The only way I’ve found to make shell II to have “a fresh look” is this:
>>> from django.db import transaction
>>> transaction.enter_transaction_management()
>>> transaction.rollback()
And I need to roolback() each time I query for fruits if I want to be sure that orm will not “lie”.
This is a issolation issue. You can relax to ‘isolation level read commited’ to avoid this behavior. Also, learn about this issolation level implications (phantoms).
To set Read Commited Isolation Level into MySQL for your django project you should set this parameter in settings.py: