Ticket #24325: 24325.diff

File 24325.diff, 1.3 KB (added by Tim Graham, 9 years ago)
  • docs/releases/1.8.txt

    diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt
    index 2516807..5ebaafb 100644
    a b Now, an error will be raised to prevent data loss::  
    710710    ...
    711711    ValueError: Cannot assign "<Author: John>": "Author" instance isn't saved in the database.
    712712
     713Accessing foreign keys in ``ModelForm.save()``
     714~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     715
     716In older versions, you could access foreign keys in ``ModelForm.save()`` when
     717adding new instances. For example, given ``Book`` with a ``ForeignKey`` to
     718``Author``::
     719
     720    class BookForm(forms.ModelForm):
     721        def save(self, *args, **kwargs):
     722            book = super(BookForm, self).save(*args, **kwargs)
     723            book.title = "%s by %s" % (book.title, book.author.name)
     724            return book
     725
     726This is no longer possible because accessing the foreign key (``book.author``
     727in the example) will raise ``RelatedObjectDoesNotExist``. This change was
     728necessary to avoid assigning unsaved objects to relations (as described in the
     729previous section).
     730
     731To adapt the above example, you could replace ``book.author.name`` with
     732``self.cleaned_data['author'].name``.
     733
    713734Management commands that only accept positional arguments
    714735~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    715736
Back to Top