Django for Designers: Difference between revisions

Content added Content deleted
imported>Paulproteus
imported>Paulproteus
(→‎Add some bookmarks via the command line: Improve verbiage around id)
Line 932: Line 932:
<source lang="python">>>> b.id</source>
<source lang="python">>>> b.id</source>


Python has given you back your prompt because the value of b.id is ''None''. That is because we have not yet saved the object; to the database, it does not exist.
What happens?


Save your bookmark to the database. In Django, you have to call save() explicitly.
Save your bookmark to the database. In Django, you have to call save() explicitly.
Line 938: Line 938:
<source lang="python">>>> b.save()</source>
<source lang="python">>>> b.save()</source>


Get the id of the Bookmark instance. Because it’s been saved, it has an ID in the database now! Even though we didn't specify one in our models.py, every saved model instance automatically has an id field.
Now, try again to get the id of the Bookmark instance. Because it’s been saved, it has an ID in the database now! Even though we didn't specify one in our models.py, every saved model instance automatically has an id field.


<source lang="python">>>> b.id
<source lang="python">>>> b.id