Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes #13538 -- Clarified query examples with more explicit import st…

…atements and model vs. instance differentiation. Thanks to yipengh87@gmail.com and kmtracey for the report, and timo for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14070 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cf9249746a79ccaab54196ab9ccd2a83bfc45e0e 1 parent a904e55
authored October 09, 2010

Showing 1 changed file with 10 additions and 5 deletions. Show diff stats Hide diff stats

  1. 15  docs/topics/db/queries.txt
15  docs/topics/db/queries.txt
@@ -94,18 +94,23 @@ the database until you explicitly call ``save()``.
94 94
 Saving ``ForeignKey`` and ``ManyToManyField`` fields
95 95
 ----------------------------------------------------
96 96
 
97  
-Updating ``ForeignKey`` fields works exactly the same way as saving a normal
98  
-field; simply assign an object of the right type to the field in question::
  97
+Updating a ``ForeignKey`` field works exactly the same way as saving a normal
  98
+field; simply assign an object of the right type to the field in question.
  99
+This example updates the ``blog`` attribute of an ``Entry`` instance ``entry``::
99 100
 
  101
+    >>> from mysite.blog.models import Entry
  102
+    >>> entry = Entry.objects.get(pk=1)
100 103
     >>> cheese_blog = Blog.objects.get(name="Cheddar Talk")
101 104
     >>> entry.blog = cheese_blog
102 105
     >>> entry.save()
103 106
 
104 107
 Updating a ``ManyToManyField`` works a little differently; use the ``add()``
105  
-method on the field to add a record to the relation::
  108
+method on the field to add a record to the relation. This example adds the
  109
+``Author`` instance ``joe`` to the ``entry`` object::
106 110
 
107  
-    >> joe = Author.objects.create(name="Joe")
108  
-    >> entry.authors.add(joe)
  111
+    >>> from mysite.blog.models import Author
  112
+    >>> joe = Author.objects.create(name="Joe")
  113
+    >>> entry.authors.add(joe)
109 114
 
110 115
 Django will complain if you try to assign or add an object of the wrong type.
111 116
 

0 notes on commit cf92497

Please sign in to comment.
Something went wrong with that request. Please try again.