diff options
Diffstat (limited to 'docs/modelforms.txt')
| -rw-r--r-- | docs/modelforms.txt | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/docs/modelforms.txt b/docs/modelforms.txt index b9f0d88165..47eaa9a769 100644 --- a/docs/modelforms.txt +++ b/docs/modelforms.txt @@ -226,7 +226,7 @@ For example:: # Create a form instance with POST data. >>> a = Author() - >>> f = AuthorForm(a, request.POST) + >>> f = AuthorForm(request.POST, instance=a) # Create and save the new author instance. There's no need to do anything else. >>> new_author = f.save() @@ -238,34 +238,34 @@ In some cases, you may not want all the model fields to appear on the generated form. There are three ways of telling ``ModelForm`` to use only a subset of the model fields: - 1. Set ``editable=False`` on the model field. As a result, *any* form - created from the model via ``ModelForm`` will not include that - field. +1. Set ``editable=False`` on the model field. As a result, *any* form + created from the model via ``ModelForm`` will not include that + field. - 2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta`` class. - This attribute, if given, should be a list of field names to include in - the form. +2. Use the ``fields`` attribute of the ``ModelForm``'s inner ``Meta`` + class. This attribute, if given, should be a list of field names + to include in the form. - 3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta`` class. - This attribute, if given, should be a list of field names to exclude - the form. +3. Use the ``exclude`` attribute of the ``ModelForm``'s inner ``Meta`` + class. This attribute, if given, should be a list of field names + to exclude from the form. - For example, if you want a form for the ``Author`` model (defined above) - that includes only the ``name`` and ``title`` fields, you would specify - ``fields`` or ``exclude`` like this:: +For example, if you want a form for the ``Author`` model (defined +above) that includes only the ``name`` and ``title`` fields, you would +specify ``fields`` or ``exclude`` like this:: - class PartialAuthorForm(ModelForm): - class Meta: - model = Author - fields = ('name', 'title') - - class PartialAuthorForm(ModelForm): - class Meta: - model = Author - exclude = ('birth_date',) + class PartialAuthorForm(ModelForm): + class Meta: + model = Author + fields = ('name', 'title') + + class PartialAuthorForm(ModelForm): + class Meta: + model = Author + exclude = ('birth_date',) - Since the Author model has only 3 fields, 'name', 'title', and - 'birth_date', the forms above will contain exactly the same fields. +Since the Author model has only 3 fields, 'name', 'title', and +'birth_date', the forms above will contain exactly the same fields. .. note:: @@ -323,17 +323,18 @@ parameter when declaring the form field:: Form inheritance ---------------- -As with the basic forms, you can extend and reuse ``ModelForms`` by inheriting -them. Normally, this will be useful if you need to declare some extra fields -or extra methods on a parent class for use in a number of forms derived from -models. For example, using the previous ``ArticleForm`` class:: + +As with basic forms, you can extend and reuse ``ModelForms`` by inheriting +them. This is useful if you need to declare extra fields or extra methods on a +parent class for use in a number of forms derived from models. For example, +using the previous ``ArticleForm`` class:: >>> class EnhancedArticleForm(ArticleForm): ... def clean_pub_date(self): ... ... -This creates a form that behaves identically to ``ArticleForm``, except there -is some extra validation and cleaning for the ``pub_date`` field. +This creates a form that behaves identically to ``ArticleForm``, except there's +some extra validation and cleaning for the ``pub_date`` field. You can also subclass the parent's ``Meta`` inner class if you want to change the ``Meta.fields`` or ``Meta.excludes`` lists:: @@ -342,17 +343,18 @@ the ``Meta.fields`` or ``Meta.excludes`` lists:: ... class Meta(ArticleForm.Meta): ... exclude = ['body'] -This adds in the extra method from the ``EnhancedArticleForm`` and modifies +This adds the extra method from the ``EnhancedArticleForm`` and modifies the original ``ArticleForm.Meta`` to remove one field. -There are a couple of things to note, however. Most of these won't normally be -of concern unless you are trying to do something tricky with subclassing. +There are a couple of things to note, however. * Normal Python name resolution rules apply. If you have multiple base classes that declare a ``Meta`` inner class, only the first one will be - used. This means the child's ``Meta``, if it exists, otherwise the + used. This means the child's ``Meta``, if it exists, otherwise the ``Meta`` of the first parent, etc. - * For technical reasons, you cannot have a subclass that is inherited from - both a ``ModelForm`` and a ``Form`` simultaneously. + * For technical reasons, a subclass cannot inherit from both a ``ModelForm`` + and a ``Form`` simultaneously. +Chances are these notes won't affect you unless you're trying to do something +tricky with subclassing. |
