diff options
| author | Gabriel Hurley <gabehr@gmail.com> | 2010-10-09 08:20:56 +0000 |
|---|---|---|
| committer | Gabriel Hurley <gabehr@gmail.com> | 2010-10-09 08:20:56 +0000 |
| commit | cf9249746a79ccaab54196ab9ccd2a83bfc45e0e (patch) | |
| tree | 5e5b43be8ee24e17fc504dc45ac87668af87d30d /docs/topics | |
| parent | a904e55859470944c006d87665074e3574da7b52 (diff) | |
Fixes #13538 -- Clarified query examples with more explicit import statements 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
Diffstat (limited to 'docs/topics')
| -rw-r--r-- | docs/topics/db/queries.txt | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt index bc72b04370..3430fcc7fd 100644 --- a/docs/topics/db/queries.txt +++ b/docs/topics/db/queries.txt @@ -94,18 +94,23 @@ the database until you explicitly call ``save()``. Saving ``ForeignKey`` and ``ManyToManyField`` fields ---------------------------------------------------- -Updating ``ForeignKey`` fields works exactly the same way as saving a normal -field; simply assign an object of the right type to the field in question:: +Updating a ``ForeignKey`` field works exactly the same way as saving a normal +field; simply assign an object of the right type to the field in question. +This example updates the ``blog`` attribute of an ``Entry`` instance ``entry``:: + >>> from mysite.blog.models import Entry + >>> entry = Entry.objects.get(pk=1) >>> cheese_blog = Blog.objects.get(name="Cheddar Talk") >>> entry.blog = cheese_blog >>> entry.save() Updating a ``ManyToManyField`` works a little differently; use the ``add()`` -method on the field to add a record to the relation:: +method on the field to add a record to the relation. This example adds the +``Author`` instance ``joe`` to the ``entry`` object:: - >> joe = Author.objects.create(name="Joe") - >> entry.authors.add(joe) + >>> from mysite.blog.models import Author + >>> joe = Author.objects.create(name="Joe") + >>> entry.authors.add(joe) Django will complain if you try to assign or add an object of the wrong type. |
