Re: How does Django know the PK of the newly created object

2007-09-23 Thread Tim Chase
http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L266 In the function that Alex linked to, the answer to your question is on line 266: backend.ops.last_insert_id(...) This varies from backend to backend, but is abstracted enough to know what the most recent insert ID

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Siah
Thanks everyone, Once again, I do not expect to know the record's ID before having saved it. But once I save it, that object DOES HAVE an id. I am assuming it was a simple insert command underneath, but do not know how this DB assigned is has become visible to Django. The django code representing

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Richard Dahl
After the object is saved, the id is set. It is not set by django, it is set by your backend database. "There's no way to tell what the value of an ID will be before you call save(), because that value is calculated by your database, not by Django." again from the db-api documentation. -ric

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Alex Koshelev
http://code.djangoproject.com/browser/django/trunk/django/db/models/base.py#L239 On 23 сент, 19:08, Siah <[EMAIL PROTECTED]> wrote: > My examples was faulty. Sorry. I meant after you save the object for > the first time, that is: > > obj = Product(name='Apple') > obj.save() > > At this point obj

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Niklas Andersson
I´m not 100% sure about Django, but in PHP there´s a function called mysql_insert_id() in the DB-API (http://se.php.net/mysql_insert_id) - maybe Django is using something similiar. /Nianbig On 9/23/07, Siah <[EMAIL PROTECTED]> wrote: > > > > > > My examples was faulty. Sorry. I meant after you s

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Siah
My examples was faulty. Sorry. I meant after you save the object for the first time, that is: obj = Product(name='Apple') obj.save() At this point obj has an ID in it. How does it know that ID? A SQL statement similar to the following must be generated: insert into product_table (id, name) valu

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Richard Dahl
Actually, from the db-api documentation. "To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. ... ... Django doesn't hit the database until you explicitly call save()." The code: obj = Product(name='Apple') obj.id will

Re: How does Django know the PK of the newly created object

2007-09-23 Thread Alex Koshelev
I think that when you create Product object django inserts new row into database and retrieves with SELECT new id. On 23 сент, 16:00, Siah <[EMAIL PROTECTED]> wrote: > Hi, > > When in a model you run something like this: > > >> obj = Product(name='Apple') > >> obj.id > > 4 > > I realize the first

How does Django know the PK of the newly created object

2007-09-23 Thread Siah
Hi, When in a model you run something like this: >> obj = Product(name='Apple') >> obj.id 4 I realize the first statement will turn into an insert table. But, how does django know of its newly assigned primary key(ID). I have a legacy database whose ID is assigned with an after trigger, and am