On Sat, 2009-03-21 at 00:17 -0700, chefsmart wrote:
[...]
> After calling save() on the model, we see that "Now it has an ID".
> 
> So Django is doing an SQL select query, something like last_insert_id,
> if I'm not wrong?

That's correct. It varies for each database backend as to which SQL we
execute, but that's the general idea. PostgreSQL (in one configuration)
and Oracle can return the inserted ID directly from the insert query; we
handle that, too.

> 
> I have looked at the source code but it's not entirely clear to me how
> Django gets the id for the object that has just been saved.

The code in question is in django/db/models/sql/subqueries.py. Look at
the InsertQuery.execute_sql() method. The path from public API to that
method is:

(1) Model.save() (django/db/models/base.py) calls
(2) Manager._insert() (django/db/models/manager.py), which calls
(3) insert_query() (django/db/models/query.py), which calls
(4) InsertQuery.execute_sql() (django/db/models/sql/subqueries.py).

There's also some interaction with the DatabaseFeatures and
DatabaseOperations in the respective django/db/backends/*/base.py files.

Does that answer your question?

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
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