summaryrefslogtreecommitdiff
path: root/docs/topics/forms
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
parentb3db6c8dcb5145f7d45eff517bcd96460475c879 (diff)
Refs #30947 -- Changed tuples to lists where appropriate.
Diffstat (limited to 'docs/topics/forms')
-rw-r--r--docs/topics/forms/media.txt44
-rw-r--r--docs/topics/forms/modelforms.txt44
2 files changed, 44 insertions, 44 deletions
diff --git a/docs/topics/forms/media.txt b/docs/topics/forms/media.txt
index 7e5a04e3d9..801d233244 100644
--- a/docs/topics/forms/media.txt
+++ b/docs/topics/forms/media.txt
@@ -56,9 +56,9 @@ Here's an example::
class CalendarWidget(forms.TextInput):
class Media:
css = {
- 'all': ('pretty.css',)
+ 'all': ['pretty.css'],
}
- js = ('animations.js', 'actions.js')
+ js = ['animations.js', 'actions.js']
This code defines a ``CalendarWidget``, which will be based on ``TextInput``.
Every time the CalendarWidget is used on a form, that form will be directed
@@ -96,8 +96,8 @@ provide two CSS options -- one for the screen, and one for print::
class Media:
css = {
- 'screen': ('pretty.css',),
- 'print': ('newspaper.css',)
+ 'screen': ['pretty.css'],
+ 'print': ['newspaper.css'],
}
If a group of CSS files are appropriate for multiple output media types,
@@ -107,9 +107,9 @@ requirements::
class Media:
css = {
- 'screen': ('pretty.css',),
- 'tv,projector': ('lo_res.css',),
- 'print': ('newspaper.css',)
+ 'screen': ['pretty.css'],
+ 'tv,projector': ['lo_res.css'],
+ 'print': ['newspaper.css],
}
If this last CSS definition were to be rendered, it would become the following HTML::
@@ -144,9 +144,9 @@ example above::
>>> class FancyCalendarWidget(CalendarWidget):
... class Media:
... css = {
- ... 'all': ('fancy.css',)
+ ... 'all': ['fancy.css'],
... }
- ... js = ('whizbang.js',)
+ ... js = ['whizbang.js']
>>> w = FancyCalendarWidget()
>>> print(w.media)
@@ -164,9 +164,9 @@ an ``extend=False`` declaration to the ``Media`` declaration::
... class Media:
... extend = False
... css = {
- ... 'all': ('fancy.css',)
+ ... 'all': ['fancy.css'],
... }
- ... js = ('whizbang.js',)
+ ... js = ['whizbang.js']
>>> w = FancyCalendarWidget()
>>> print(w.media)
@@ -195,8 +195,8 @@ be defined in a dynamic fashion::
class CalendarWidget(forms.TextInput):
@property
def media(self):
- return forms.Media(css={'all': ('pretty.css',)},
- js=('animations.js', 'actions.js'))
+ return forms.Media(css={'all': ['pretty.css']},
+ js=['animations.js', 'actions.js'])
See the section on `Media objects`_ for more details on how to construct
return values for dynamic ``media`` properties.
@@ -230,9 +230,9 @@ was ``None``::
>>> class CalendarWidget(forms.TextInput):
... class Media:
... css = {
- ... 'all': ('/css/pretty.css',),
+ ... 'all': ['/css/pretty.css'],
... }
- ... js = ('animations.js', 'http://othersite.com/actions.js')
+ ... js = ['animations.js', 'http://othersite.com/actions.js']
>>> w = CalendarWidget()
>>> print(w.media)
@@ -277,7 +277,7 @@ outputting the complete HTML ``<script>`` or ``<link>`` tag content::
>>> class SomeWidget(forms.TextInput):
... class Media:
- ... js = (JSPath(),)
+ ... js = [JSPath()]
``Media`` objects
=================
@@ -319,13 +319,13 @@ specified by both::
>>> class CalendarWidget(forms.TextInput):
... class Media:
... css = {
- ... 'all': ('pretty.css',)
+ ... 'all': ['pretty.css'],
... }
- ... js = ('animations.js', 'actions.js')
+ ... js = ['animations.js', 'actions.js']
>>> class OtherWidget(forms.TextInput):
... class Media:
- ... js = ('whizbang.js',)
+ ... js = ['whizbang.js']
>>> w1 = CalendarWidget()
>>> w2 = OtherWidget()
@@ -350,10 +350,10 @@ For example::
>>> from django import forms
>>> class CalendarWidget(forms.TextInput):
... class Media:
- ... js = ('jQuery.js', 'calendar.js', 'noConflict.js')
+ ... js = ['jQuery.js', 'calendar.js', 'noConflict.js']
>>> class TimeWidget(forms.TextInput):
... class Media:
- ... js = ('jQuery.js', 'time.js', 'noConflict.js')
+ ... js = ['jQuery.js', 'time.js', 'noConflict.js']
>>> w1 = CalendarWidget()
>>> w2 = TimeWidget()
>>> print(w1.media + w2.media)
@@ -400,7 +400,7 @@ CSS for form layout -- add a ``Media`` declaration to the form::
...
... class Media:
... css = {
- ... 'all': ('layout.css',)
+ ... 'all': ['layout.css'],
... }
>>> f = ContactForm()
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():