From edcc75e5ac5b9dc2f174580e7adacd3be586f8bd Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 5 Aug 2014 08:40:27 -0400 Subject: [PATCH] Fixed #21792 -- Documented Form.has_changed() Thanks bjb at credil.org for the suggestion and Ivan Mesic for the draft patch. --- docs/ref/forms/api.txt | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 954d14e184..42780bde3c 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -256,6 +256,31 @@ precedence:: Url: Comment: +Checking if form data has changed +--------------------------------- + +.. method:: Form.has_changed() + +Use the ``has_changed()`` method on your ``Form`` when you need to check if the +form data has been changed from the initial data. + + >>> data = {'subject': 'hello', + ... 'message': 'Hi there', + ... 'sender': 'foo@example.com', + ... 'cc_myself': True} + >>> f = ContactForm(initial=data) + >>> f.has_changed() + False + +When the form is submitted, we reconstruct it and provide the original data +so that the comparison can be done: + + >>> f = ContactForm(request.POST, initial=data) + >>> f.has_changed() + +``has_changed()`` will be ``True`` if the data from ``request.POST`` differs +from what was provided in :attr:`~Form.initial` or ``False`` otherwise. + Accessing the fields from the form ----------------------------------