From 6667a5dff0658801f6c6b8c23b514924937b70c6 Mon Sep 17 00:00:00 2001
From: Derek Willis <dwillis@gmail.com>
Date: Sun, 1 Aug 2010 21:36:41 -0400
Subject: [PATCH] Clarified Saving ForeignKey and ManyToManyField fields docs
---
docs/topics/db/queries.txt | 9 +++++++--
1 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
index 981d727..7bb66fe 100644
|
a
|
b
|
Saving ``ForeignKey`` and ``ManyToManyField`` fields
|
| 97 | 97 | ---------------------------------------------------- |
| 98 | 98 | |
| 99 | 99 | Updating ``ForeignKey`` fields works exactly the same way as saving a normal |
| 100 | | field; simply assign an object of the right type to the field in question:: |
| | 100 | field; simply assign an object of the right type to the field in question. |
| | 101 | Given an ``Entry`` instance ``entry``, this example updates its blog attribute:: |
| 101 | 102 | |
| | 103 | >>> from mysite.blog.models import Entry |
| | 104 | >>> entry = Entry.objects.get(pk=1) |
| 102 | 105 | >>> cheese_blog = Blog.objects.get(name="Cheddar Talk") |
| 103 | 106 | >>> entry.blog = cheese_blog |
| 104 | 107 | >>> entry.save() |
| 105 | 108 | |
| 106 | 109 | Updating a ``ManyToManyField`` works a little differently; use the ``add()`` |
| 107 | | method on the field to add a record to the relation:: |
| | 110 | method on the field to add a record to the relation. This example adds the |
| | 111 | ``Author`` instance ``joe`` to the entry object:: |
| 108 | 112 | |
| | 113 | >> from mysite.blog.models import Author |
| 109 | 114 | >> joe = Author.objects.create(name="Joe") |
| 110 | 115 | >> entry.authors.add(joe) |
| 111 | 116 | |