On Wed, Sep 9, 2009 at 4:08 AM, Daniel Roseman <dan...@roseman.org.uk>wrote:

>
> As regards updating, Django will only update the fields that have
> changed in any case.
>

I don't believe this is true.  Only updating the fields that have changed
would require keeping track of what fields in the Python model instance have
been modified since it was created, and Django doesn't do that.
Alternatively the existing record could be read just prior to update and
compared with what's currently in the Python model to implement updating
only fields that differ, but Django doesn't do that either.  It does check
for the existence of a record with the to-be-updated model instance's
primary key, but it includes in the update call all non-pk fields in the
model.  The code involved is:
http://code.djangoproject.com/browser/django/tags/releases/1.1/django/db/models/base.py#L467
.

Running a little test in the shell and looking at the SQL queries confirms
that all fields are listed in the SQL UPDATE, not just changed ones:

>>> from django.db import connection
>>> from ttt.models import Foo
>>> x = Foo.objects.all()[0]
>>> x.i = 44
>>> x.save()
>>> from pprint import pprint
>>> pprint(connection.queries)
[{'sql': u'SELECT "ttt_foo"."id", "ttt_foo"."name", "ttt_foo"."i" FROM
"ttt_foo" LIMIT 1',
  'time': '0.010'},
 {'sql': u'SELECT (1) AS "a" FROM "ttt_foo" WHERE "ttt_foo"."id" = 1 ',
  'time': '0.000'},
 {'sql': u'UPDATE "ttt_foo" SET "name" = x, "i" = 44 WHERE "ttt_foo"."id" =
1 ',
  'time': '0.000'}]
>>>

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to