Fixed #21516 -- Updated imports paths for some formset functions/classes.

Since refs #21489, FormSet classes and factories are exposed on the
django.forms package.
This commit is contained in:
Bryan Marty 2015-10-27 18:37:35 -07:00 committed by Tim Graham
parent dbe79d9660
commit 455034d4df
3 changed files with 29 additions and 29 deletions

View File

@ -1681,7 +1681,7 @@ templates used by the :class:`ModelAdmin` views:
changelist page if :attr:`~ModelAdmin.list_editable` is used. To use a changelist page if :attr:`~ModelAdmin.list_editable` is used. To use a
custom formset, for example:: custom formset, for example::
from django.forms.models import BaseModelFormSet from django.forms import BaseModelFormSet
class MyAdminFormSet(BaseModelFormSet): class MyAdminFormSet(BaseModelFormSet):
pass pass

View File

@ -20,7 +20,7 @@ form::
You might want to allow the user to create several articles at once. To create You might want to allow the user to create several articles at once. To create
a formset out of an ``ArticleForm`` you would do:: a formset out of an ``ArticleForm`` you would do::
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> ArticleFormSet = formset_factory(ArticleForm) >>> ArticleFormSet = formset_factory(ArticleForm)
You now have created a formset named ``ArticleFormSet``. The formset gives you You now have created a formset named ``ArticleFormSet``. The formset gives you
@ -60,7 +60,7 @@ number of forms it generates from the initial data. Let's take a look at an
example:: example::
>>> import datetime >>> import datetime
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, extra=2) >>> ArticleFormSet = formset_factory(ArticleForm, extra=2)
>>> formset = ArticleFormSet(initial=[ >>> formset = ArticleFormSet(initial=[
@ -93,7 +93,7 @@ Limiting the maximum number of forms
The ``max_num`` parameter to :func:`~django.forms.formsets.formset_factory` The ``max_num`` parameter to :func:`~django.forms.formsets.formset_factory`
gives you the ability to limit the number of forms the formset will display:: gives you the ability to limit the number of forms the formset will display::
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1) >>> ArticleFormSet = formset_factory(ArticleForm, extra=2, max_num=1)
>>> formset = ArticleFormSet() >>> formset = ArticleFormSet()
@ -130,7 +130,7 @@ Validation with a formset is almost identical to a regular ``Form``. There is
an ``is_valid`` method on the formset to provide a convenient way to validate an ``is_valid`` method on the formset to provide a convenient way to validate
all forms in the formset:: all forms in the formset::
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm) >>> ArticleFormSet = formset_factory(ArticleForm)
>>> data = { >>> data = {
@ -253,8 +253,8 @@ Custom formset validation
A formset has a ``clean`` method similar to the one on a ``Form`` class. This A formset has a ``clean`` method similar to the one on a ``Form`` class. This
is where you define your own validation that works at the formset level:: is where you define your own validation that works at the formset level::
>>> from django.forms.formsets import BaseFormSet >>> from django.forms import BaseFormSet
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> class BaseArticleFormSet(BaseFormSet): >>> class BaseArticleFormSet(BaseFormSet):
@ -309,7 +309,7 @@ If ``validate_max=True`` is passed to
that the number of forms in the data set, minus those marked for that the number of forms in the data set, minus those marked for
deletion, is less than or equal to ``max_num``. deletion, is less than or equal to ``max_num``.
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, max_num=1, validate_max=True) >>> ArticleFormSet = formset_factory(ArticleForm, max_num=1, validate_max=True)
>>> data = { >>> data = {
@ -351,7 +351,7 @@ If ``validate_min=True`` is passed to
that the number of forms in the data set, minus those marked for that the number of forms in the data set, minus those marked for
deletion, is greater than or equal to ``min_num``. deletion, is greater than or equal to ``min_num``.
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, min_num=3, validate_min=True) >>> ArticleFormSet = formset_factory(ArticleForm, min_num=3, validate_min=True)
>>> data = { >>> data = {
@ -388,7 +388,7 @@ Default: ``False``
Lets you create a formset with the ability to order:: Lets you create a formset with the ability to order::
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, can_order=True) >>> ArticleFormSet = formset_factory(ArticleForm, can_order=True)
>>> formset = ArticleFormSet(initial=[ >>> formset = ArticleFormSet(initial=[
@ -448,7 +448,7 @@ Default: ``False``
Lets you create a formset with the ability to select forms for deletion:: Lets you create a formset with the ability to select forms for deletion::
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> ArticleFormSet = formset_factory(ArticleForm, can_delete=True) >>> ArticleFormSet = formset_factory(ArticleForm, can_delete=True)
>>> formset = ArticleFormSet(initial=[ >>> formset = ArticleFormSet(initial=[
@ -519,8 +519,8 @@ accomplished. The formset base class provides an ``add_fields`` method. You
can simply override this method to add your own fields or even redefine the can simply override this method to add your own fields or even redefine the
default fields/attributes of the order and deletion fields:: default fields/attributes of the order and deletion fields::
>>> from django.forms.formsets import BaseFormSet >>> from django.forms import BaseFormSet
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> class BaseArticleFormSet(BaseFormSet): >>> class BaseArticleFormSet(BaseFormSet):
... def add_fields(self, form, index): ... def add_fields(self, form, index):
@ -543,8 +543,8 @@ Passing custom parameters to formset forms
Sometimes your form class takes custom parameters, like ``MyArticleForm``. Sometimes your form class takes custom parameters, like ``MyArticleForm``.
You can pass this parameter when instantiating the formset:: You can pass this parameter when instantiating the formset::
>>> from django.forms.formsets import BaseFormSet >>> from django.forms import BaseFormSet
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> from myapp.forms import ArticleForm >>> from myapp.forms import ArticleForm
>>> class MyArticleForm(ArticleForm): >>> class MyArticleForm(ArticleForm):
@ -560,8 +560,8 @@ base class provides a ``get_form_kwargs`` method. The method takes a single
argument - the index of the form in the formset. The index is ``None`` for the argument - the index of the form in the formset. The index is ``None`` for the
:ref:`empty_form`:: :ref:`empty_form`::
>>> from django.forms.formsets import BaseFormSet >>> from django.forms import BaseFormSet
>>> from django.forms.formsets import formset_factory >>> from django.forms import formset_factory
>>> class BaseArticleFormSet(BaseFormSet): >>> class BaseArticleFormSet(BaseFormSet):
... def get_form_kwargs(self, index): ... def get_form_kwargs(self, index):
@ -581,7 +581,7 @@ Using a formset inside a view is as easy as using a regular ``Form`` class.
The only thing you will want to be aware of is making sure to use the The only thing you will want to be aware of is making sure to use the
management form inside the template. Let's look at a sample view:: management form inside the template. Let's look at a sample view::
from django.forms.formsets import formset_factory from django.forms import formset_factory
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from myapp.forms import ArticleForm from myapp.forms import ArticleForm
@ -658,7 +658,7 @@ borrow much of its behavior from forms. With that said you are able to use
more than one formset to be sent to a view without name clashing. Lets take more than one formset to be sent to a view without name clashing. Lets take
a look at how this might be accomplished:: a look at how this might be accomplished::
from django.forms.formsets import formset_factory from django.forms import formset_factory
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from myapp.forms import ArticleForm, BookForm from myapp.forms import ArticleForm, BookForm

View File

@ -698,7 +698,7 @@ You can create forms from a given model using the standalone function
definition. This may be more convenient if you do not have many customizations definition. This may be more convenient if you do not have many customizations
to make:: to make::
>>> from django.forms.models import modelform_factory >>> from django.forms import modelform_factory
>>> from myapp.models import Book >>> from myapp.models import Book
>>> BookForm = modelform_factory(Book, fields=("author", "title")) >>> BookForm = modelform_factory(Book, fields=("author", "title"))
@ -729,7 +729,7 @@ Like :doc:`regular formsets </topics/forms/formsets>`, Django provides a couple
of enhanced formset classes that make it easy to work with Django models. Let's of enhanced formset classes that make it easy to work with Django models. Let's
reuse the ``Author`` model from above:: reuse the ``Author`` model from above::
>>> from django.forms.models import modelformset_factory >>> from django.forms import modelformset_factory
>>> from myapp.models import Author >>> from myapp.models import Author
>>> AuthorFormSet = modelformset_factory(Author, fields=('name', 'title')) >>> AuthorFormSet = modelformset_factory(Author, fields=('name', 'title'))
@ -773,7 +773,7 @@ queryset that includes all objects in the model (e.g.,
Alternatively, you can create a subclass that sets ``self.queryset`` in Alternatively, you can create a subclass that sets ``self.queryset`` in
``__init__``:: ``__init__``::
from django.forms.models import BaseModelFormSet from django.forms import BaseModelFormSet
from myapp.models import Author from myapp.models import Author
class BaseAuthorFormSet(BaseModelFormSet): class BaseAuthorFormSet(BaseModelFormSet):
@ -941,7 +941,7 @@ Using a model formset in a view
Model formsets are very similar to formsets. Let's say we want to present a Model formsets are very similar to formsets. Let's say we want to present a
formset to edit ``Author`` model instances:: formset to edit ``Author`` model instances::
from django.forms.models import modelformset_factory from django.forms import modelformset_factory
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from myapp.models import Author from myapp.models import Author
@ -975,7 +975,7 @@ the unique constraints on your model (either ``unique``, ``unique_together`` or
on a ``ModelFormSet`` and maintain this validation, you must call the parent on a ``ModelFormSet`` and maintain this validation, you must call the parent
class's ``clean`` method:: class's ``clean`` method::
from django.forms.models import BaseModelFormSet from django.forms import BaseModelFormSet
class MyModelFormSet(BaseModelFormSet): class MyModelFormSet(BaseModelFormSet):
def clean(self): def clean(self):
@ -991,7 +991,7 @@ have already been created for each ``Form``. Modifying a value in
to modify a value in ``ModelFormSet.clean()`` you must modify to modify a value in ``ModelFormSet.clean()`` you must modify
``form.instance``:: ``form.instance``::
from django.forms.models import BaseModelFormSet from django.forms import BaseModelFormSet
class MyModelFormSet(BaseModelFormSet): class MyModelFormSet(BaseModelFormSet):
def clean(self): def clean(self):
@ -1009,7 +1009,7 @@ Using a custom queryset
As stated earlier, you can override the default queryset used by the model As stated earlier, you can override the default queryset used by the model
formset:: formset::
from django.forms.models import modelformset_factory from django.forms import modelformset_factory
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from myapp.models import Author from myapp.models import Author
@ -1113,7 +1113,7 @@ you have these two models::
If you want to create a formset that allows you to edit books belonging to If you want to create a formset that allows you to edit books belonging to
a particular author, you could do this:: a particular author, you could do this::
>>> from django.forms.models import inlineformset_factory >>> from django.forms import inlineformset_factory
>>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',)) >>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',))
>>> author = Author.objects.get(name='Mike Royko') >>> author = Author.objects.get(name='Mike Royko')
>>> formset = BookFormSet(instance=author) >>> formset = BookFormSet(instance=author)
@ -1137,7 +1137,7 @@ When overriding methods on ``InlineFormSet``, you should subclass
For example, if you want to override ``clean()``:: For example, if you want to override ``clean()``::
from django.forms.models import BaseInlineFormSet from django.forms import BaseInlineFormSet
class CustomInlineFormSet(BaseInlineFormSet): class CustomInlineFormSet(BaseInlineFormSet):
def clean(self): def clean(self):
@ -1152,7 +1152,7 @@ See also :ref:`model-formsets-overriding-clean`.
Then when you create your inline formset, pass in the optional argument Then when you create your inline formset, pass in the optional argument
``formset``:: ``formset``::
>>> from django.forms.models import inlineformset_factory >>> from django.forms import inlineformset_factory
>>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',), >>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',),
... formset=CustomInlineFormSet) ... formset=CustomInlineFormSet)
>>> author = Author.objects.get(name='Mike Royko') >>> author = Author.objects.get(name='Mike Royko')