Have a look at this -
>>> from apps.users.models import Member
>>> members = Member.objects.all()
>>> members[1].user_type
u'C'
>>> members[1].user_type = 'M'
>>> members[1].save()
>>> members[1].user_type
u'C'
>>> m = members[1]
>>> m.user_type
u'C'
>>> m.user_type = 'M'
>>> m.save()
>>> m.user_type
'M'
here is what I think is happening: The all()
method returns a QuerySet. And from the queries above, the changes are not being committed to database when you execute save()
on an item in the QuerySet. But it does work if you do it on a Member
object individually. And from Django documentation -
QuerySets are lazy -- the act of creating a QuerySet doesn't involve any database activity. You can stack filters together all day long, and Django won't actually run the query until the QuerySet is evaluated. ...
In general, the results of a QuerySet aren't fetched from the database until you "ask" for them. When you do, the QuerySet is evaluated by accessing the database.
You can read more here. So the save()
method on members[1]
doesn't touch the database or read back from there. While the changes on the Member
objects are committed to database and read back immediately.