Ticket #13538: 13538.2.diff
File 13538.2.diff, 1.3 KB (added by , 14 years ago) |
---|
-
docs/topics/db/queries.txt
95 95 ---------------------------------------------------- 96 96 97 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:: 98 field; simply assign an object of the right type to the field in question. 99 Given an ``Entry`` instance ``entry``, this example updates its blog attribute:: 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