Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
74e1980c
Kaydet (Commit)
74e1980c
authored
Eki 27, 2014
tarafından
Peter Inglesby
Kaydeden (comit)
Tim Graham
Kas 04, 2014
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #13181 -- Added support for callable choices to forms.ChoiceField
Thanks vanschelven and expleo for the initial patch.
üst
e0685368
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
5 deletions
+50
-5
fields.py
django/forms/fields.py
+15
-1
fields.txt
docs/ref/forms/fields.txt
+10
-4
1.8.txt
docs/releases/1.8.txt
+3
-0
test_fields.py
tests/forms_tests/tests/test_fields.py
+22
-0
No files found.
django/forms/fields.py
Dosyayı görüntüle @
74e1980c
...
...
@@ -798,6 +798,15 @@ class NullBooleanField(BooleanField):
return
initial
!=
data
class
CallableChoiceIterator
(
object
):
def
__init__
(
self
,
choices_func
):
self
.
choices_func
=
choices_func
def
__iter__
(
self
):
for
e
in
self
.
choices_func
():
yield
e
class
ChoiceField
(
Field
):
widget
=
Select
default_error_messages
=
{
...
...
@@ -822,7 +831,12 @@ class ChoiceField(Field):
# Setting choices also sets the choices on the widget.
# choices can be any iterable, but we call list() on it because
# it will be consumed more than once.
self
.
_choices
=
self
.
widget
.
choices
=
list
(
value
)
if
callable
(
value
):
value
=
CallableChoiceIterator
(
value
)
else
:
value
=
list
(
value
)
self
.
_choices
=
self
.
widget
.
choices
=
value
choices
=
property
(
_get_choices
,
_set_choices
)
...
...
docs/ref/forms/fields.txt
Dosyayı görüntüle @
74e1980c
...
...
@@ -387,10 +387,16 @@ For each field, we describe the default widget used if you don't specify
.. attribute:: choices
An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this
field. This argument accepts the same formats as the ``choices`` argument
to a model field. See the :ref:`model field reference documentation on
choices <field-choices>` for more details.
Either an iterable (e.g., a list or tuple) of 2-tuples to use as
choices for this field, or a callable that returns such an iterable.
This argument accepts the same formats as the ``choices`` argument to a
model field. See the :ref:`model field reference documentation on
choices <field-choices>` for more details. If the argument is a
callable, it is evaluated each time the field's form is initialized.
.. versionchanged:: 1.8
The ability to pass a callable to ``choices`` was added.
``TypedChoiceField``
~~~~~~~~~~~~~~~~~~~~
...
...
docs/releases/1.8.txt
Dosyayı görüntüle @
74e1980c
...
...
@@ -240,6 +240,9 @@ Forms
will also update ``UploadedFile.content_type`` with the image's content type
as determined by Pillow.
* You can now pass a callable that returns an iterable of choices when
instantiating a :class:`~django.forms.ChoiceField`.
Generic Views
^^^^^^^^^^^^^
...
...
tests/forms_tests/tests/test_fields.py
Dosyayı görüntüle @
74e1980c
...
...
@@ -961,6 +961,28 @@ class FieldsTests(SimpleTestCase):
self
.
assertEqual
(
'5'
,
f
.
clean
(
'5'
))
self
.
assertRaisesMessage
(
ValidationError
,
"'Select a valid choice. 6 is not one of the available choices.'"
,
f
.
clean
,
'6'
)
def
test_choicefield_callable
(
self
):
choices
=
lambda
:
[(
'J'
,
'John'
),
(
'P'
,
'Paul'
)]
f
=
ChoiceField
(
choices
=
choices
)
self
.
assertEqual
(
'J'
,
f
.
clean
(
'J'
))
def
test_choicefield_callable_may_evaluate_to_different_values
(
self
):
choices
=
[]
def
choices_as_callable
():
return
choices
class
ChoiceFieldForm
(
Form
):
choicefield
=
ChoiceField
(
choices
=
choices_as_callable
)
choices
=
[(
'J'
,
'John'
)]
form
=
ChoiceFieldForm
()
self
.
assertEqual
([(
'J'
,
'John'
)],
list
(
form
.
fields
[
'choicefield'
]
.
choices
))
choices
=
[(
'P'
,
'Paul'
)]
form
=
ChoiceFieldForm
()
self
.
assertEqual
([(
'P'
,
'Paul'
)],
list
(
form
.
fields
[
'choicefield'
]
.
choices
))
# TypedChoiceField ############################################################
# TypedChoiceField is just like ChoiceField, except that coerced types will
# be returned:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment