Ticket #13538: 13538.2.diff

File 13538.2.diff, 1.3 KB (added by timo, 5 years ago)

updating existing patch to fix syntax highlighting

  • docs/topics/db/queries.txt

     
    9595----------------------------------------------------
    9696
    9797Updating ``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::
     98field; simply assign an object of the right type to the field in question.
     99Given an ``Entry`` instance ``entry``, this example updates its blog attribute::
    99100
     101    >>> from mysite.blog.models import Entry
     102    >>> entry = Entry.objects.get(pk=1)
    100103    >>> cheese_blog = Blog.objects.get(name="Cheddar Talk")
    101104    >>> entry.blog = cheese_blog
    102105    >>> entry.save()
    103106
    104107Updating a ``ManyToManyField`` works a little differently; use the ``add()``
    105 method on the field to add a record to the relation::
     108method on the field to add a record to the relation. This example adds the
     109``Author`` instance ``joe`` to the entry object::
    106110
    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)
    109114
    110115Django will complain if you try to assign or add an object of the wrong type.
    111116
Back to Top