summaryrefslogtreecommitdiff
path: root/docs/topics/forms/modelforms.txt
diff options
context:
space:
mode:
authorAlex Morega <alex@grep.ro>2022-08-26 17:10:27 +0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-08-30 09:57:17 +0200
commitde6c9c70549010fc39509f9ef3f6a62ada870318 (patch)
treefc0206ae8cd3fc3d7a78e76b6ae3e95f4e0505b1 /docs/topics/forms/modelforms.txt
parentb3db6c8dcb5145f7d45eff517bcd96460475c879 (diff)
Refs #30947 -- Changed tuples to lists where appropriate.
Diffstat (limited to 'docs/topics/forms/modelforms.txt')
-rw-r--r--docs/topics/forms/modelforms.txt44
1 files changed, 22 insertions, 22 deletions
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
index 9000ff24f4..349162abd1 100644
--- a/docs/topics/forms/modelforms.txt
+++ b/docs/topics/forms/modelforms.txt
@@ -514,7 +514,7 @@ For example, if you want the ``CharField`` for the ``name`` attribute of
class AuthorForm(ModelForm):
class Meta:
model = Author
- fields = ('name', 'title', 'birth_date')
+ fields = ['name', 'title', 'birth_date']
widgets = {
'name': Textarea(attrs={'cols': 80, 'rows': 20}),
}
@@ -535,7 +535,7 @@ the ``name`` field::
class AuthorForm(ModelForm):
class Meta:
model = Author
- fields = ('name', 'title', 'birth_date')
+ fields = ['name', 'title', 'birth_date']
labels = {
'name': _('Writer'),
}
@@ -669,7 +669,7 @@ attribute on the ``Meta`` class.
>>> class AuthorForm(ModelForm):
... class Meta:
... model = Author
- ... localized_fields = ('birth_date',)
+ ... localized_fields = ['birth_date']
If ``localized_fields`` is set to the special value ``'__all__'``, all fields
will be localized.
@@ -694,7 +694,7 @@ the ``Meta.fields`` or ``Meta.exclude`` lists::
>>> class RestrictedArticleForm(EnhancedArticleForm):
... class Meta(ArticleForm.Meta):
- ... exclude = ('body',)
+ ... exclude = ['body']
This adds the extra method from the ``EnhancedArticleForm`` and modifies
the original ``ArticleForm.Meta`` to remove one field.
@@ -746,7 +746,7 @@ to make::
>>> from django.forms import modelform_factory
>>> from myapp.models import Book
- >>> BookForm = modelform_factory(Book, fields=("author", "title"))
+ >>> BookForm = modelform_factory(Book, fields=["author", "title"])
This can also be used to make modifications to existing forms, for example by
specifying the widgets to be used for a given field::
@@ -762,7 +762,7 @@ documentation.
... or enable localization for specific fields::
- >>> Form = modelform_factory(Author, form=AuthorForm, localized_fields=("birth_date",))
+ >>> Form = modelform_factory(Author, form=AuthorForm, localized_fields=["birth_date"])
.. _model-formsets:
@@ -777,13 +777,13 @@ convenient. Let's reuse the ``Author`` model from above::
>>> from django.forms import modelformset_factory
>>> from myapp.models import Author
- >>> AuthorFormSet = modelformset_factory(Author, fields=('name', 'title'))
+ >>> AuthorFormSet = modelformset_factory(Author, fields=['name', 'title'])
Using ``fields`` restricts the formset to use only the given fields.
Alternatively, you can take an "opt-out" approach, specifying which fields to
exclude::
- >>> AuthorFormSet = modelformset_factory(Author, exclude=('birth_date',))
+ >>> AuthorFormSet = modelformset_factory(Author, exclude=['birth_date'])
This will create a formset that is capable of working with the data associated
with the ``Author`` model. It works just like a regular formset::
@@ -836,7 +836,7 @@ Alternatively, you can create a subclass that sets ``self.queryset`` in
Then, pass your ``BaseAuthorFormSet`` class to the factory function::
>>> AuthorFormSet = modelformset_factory(
- ... Author, fields=('name', 'title'), formset=BaseAuthorFormSet)
+ ... Author, fields=['name', 'title'], formset=BaseAuthorFormSet)
If you want to return a formset that doesn't include *any* preexisting
instances of the model, you can specify an empty QuerySet::
@@ -854,7 +854,7 @@ you can create a custom model form that has custom validation::
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
- fields = ('name', 'title')
+ fields = ['name', 'title']
def clean_name(self):
# custom validation for the name field
@@ -877,7 +877,7 @@ works the same way as the ``widgets`` dictionary on the inner ``Meta``
class of a ``ModelForm`` works::
>>> AuthorFormSet = modelformset_factory(
- ... Author, fields=('name', 'title'),
+ ... Author, fields=['name', 'title'],
... widgets={'name': Textarea(attrs={'cols': 80, 'rows': 20})})
Enabling localization for fields with ``localized_fields``
@@ -887,8 +887,8 @@ Using the ``localized_fields`` parameter, you can enable localization for
fields in the form.
>>> AuthorFormSet = modelformset_factory(
- ... Author, fields=('name', 'title', 'birth_date'),
- ... localized_fields=('birth_date',))
+ ... Author, fields=['name', 'title', 'birth_date'],
+ ... localized_fields=['birth_date'])
If ``localized_fields`` is set to the special value ``'__all__'``, all fields
will be localized.
@@ -964,7 +964,7 @@ extra forms displayed.
>>> Author.objects.order_by('name')
<QuerySet [<Author: Charles Baudelaire>, <Author: Paul Verlaine>, <Author: Walt Whitman>]>
- >>> AuthorFormSet = modelformset_factory(Author, fields=('name',), max_num=1)
+ >>> AuthorFormSet = modelformset_factory(Author, fields=['name'], max_num=1)
>>> formset = AuthorFormSet(queryset=Author.objects.order_by('name'))
>>> [x.name for x in formset.get_queryset()]
['Charles Baudelaire', 'Paul Verlaine', 'Walt Whitman']
@@ -978,7 +978,7 @@ If the value of ``max_num`` is greater than the number of existing related
objects, up to ``extra`` additional blank forms will be added to the formset,
so long as the total number of forms does not exceed ``max_num``::
- >>> AuthorFormSet = modelformset_factory(Author, fields=('name',), max_num=4, extra=2)
+ >>> AuthorFormSet = modelformset_factory(Author, fields=['name'], max_num=4, extra=2)
>>> formset = AuthorFormSet(queryset=Author.objects.order_by('name'))
>>> for form in formset:
... print(form.as_table())
@@ -1002,7 +1002,7 @@ objects::
>>> AuthorFormSet = modelformset_factory(
... Author,
- ... fields=('name', 'title'),
+ ... fields=['name', 'title'],
... edit_only=True,
... )
@@ -1020,7 +1020,7 @@ formset to edit ``Author`` model instances::
from myapp.models import Author
def manage_authors(request):
- AuthorFormSet = modelformset_factory(Author, fields=('name', 'title'))
+ AuthorFormSet = modelformset_factory(Author, fields=['name', 'title'])
if request.method == 'POST':
formset = AuthorFormSet(request.POST, request.FILES)
if formset.is_valid():
@@ -1086,7 +1086,7 @@ formset::
from myapp.models import Author
def manage_authors(request):
- AuthorFormSet = modelformset_factory(Author, fields=('name', 'title'))
+ AuthorFormSet = modelformset_factory(Author, fields=['name', 'title'])
queryset = Author.objects.filter(name__startswith='O')
if request.method == "POST":
formset = AuthorFormSet(
@@ -1187,7 +1187,7 @@ If you want to create a formset that allows you to edit books belonging to
a particular author, you could do this::
>>> 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')
>>> formset = BookFormSet(instance=author)
@@ -1230,7 +1230,7 @@ Then when you create your inline formset, pass in the optional argument
``formset``::
>>> from django.forms import inlineformset_factory
- >>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',),
+ >>> BookFormSet = inlineformset_factory(Author, Book, fields=['title'],
... formset=CustomInlineFormSet)
>>> author = Author.objects.get(name='Mike Royko')
>>> formset = BookFormSet(instance=author)
@@ -1259,7 +1259,7 @@ To resolve this, you can use ``fk_name`` to
:func:`~django.forms.models.inlineformset_factory`::
>>> FriendshipFormSet = inlineformset_factory(Friend, Friendship, fk_name='from_friend',
- ... fields=('to_friend', 'length_in_months'))
+ ... fields=['to_friend', 'length_in_months'])
Using an inline formset in a view
---------------------------------
@@ -1269,7 +1269,7 @@ of a model. Here's how you can do that::
def manage_books(request, author_id):
author = Author.objects.get(pk=author_id)
- BookInlineFormSet = inlineformset_factory(Author, Book, fields=('title',))
+ BookInlineFormSet = inlineformset_factory(Author, Book, fields=['title'])
if request.method == "POST":
formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
if formset.is_valid():