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
74105b26
Kaydet (Commit)
74105b26
authored
Agu 03, 2016
tarafından
Alex Hill
Kaydeden (comit)
Tim Graham
Agu 08, 2016
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #27002 -- Prevented double query when rendering ModelChoiceField.
üst
29a3f8b4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
11 deletions
+27
-11
boundfield.py
django/forms/boundfield.py
+13
-8
tests.py
tests/model_forms/tests.py
+14
-3
No files found.
django/forms/boundfield.py
Dosyayı görüntüle @
74105b26
...
...
@@ -8,6 +8,7 @@ from django.utils import six
from
django.utils.encoding
import
(
force_text
,
python_2_unicode_compatible
,
smart_text
,
)
from
django.utils.functional
import
cached_property
from
django.utils.html
import
conditional_escape
,
format_html
,
html_safe
from
django.utils.safestring
import
mark_safe
from
django.utils.translation
import
ugettext_lazy
as
_
...
...
@@ -42,28 +43,32 @@ class BoundField(object):
return
self
.
as_widget
()
+
self
.
as_hidden
(
only_initial
=
True
)
return
self
.
as_widget
()
def
__iter__
(
self
):
@cached_property
def
subwidgets
(
self
):
"""
Yields rendered strings that comprise all widgets in this BoundField.
Most widgets yield a single subwidget, but others like RadioSelect and
CheckboxSelectMultiple produce one subwidget for each choice.
This
really is only useful for RadioSelect widgets, so that you ca
n
iterate over individual radio buttons in a template
.
This
property is cached so that only one database query occurs whe
n
rendering ModelChoiceFields
.
"""
id_
=
self
.
field
.
widget
.
attrs
.
get
(
'id'
)
or
self
.
auto_id
attrs
=
{
'id'
:
id_
}
if
id_
else
{}
attrs
=
self
.
build_widget_attrs
(
attrs
)
for
subwidget
in
self
.
field
.
widget
.
subwidgets
(
self
.
html_name
,
self
.
value
(),
attrs
):
yield
subwidget
return
list
(
self
.
field
.
widget
.
subwidgets
(
self
.
html_name
,
self
.
value
(),
attrs
))
def
__iter__
(
self
):
return
iter
(
self
.
subwidgets
)
def
__len__
(
self
):
return
len
(
list
(
self
.
__iter__
())
)
return
len
(
self
.
subwidgets
)
def
__getitem__
(
self
,
idx
):
# Prevent unnecessary reevaluation when accessing BoundField's attrs
# from templates.
if
not
isinstance
(
idx
,
six
.
integer_types
+
(
slice
,)):
raise
TypeError
return
list
(
self
.
__iter__
())
[
idx
]
return
self
.
subwidgets
[
idx
]
@property
def
errors
(
self
):
...
...
tests/model_forms/tests.py
Dosyayı görüntüle @
74105b26
...
...
@@ -1576,11 +1576,22 @@ class ModelChoiceFieldTests(TestCase):
field
=
CustomModelChoiceField
(
Category
.
objects
.
all
())
self
.
assertIsInstance
(
field
.
choices
,
CustomModelChoiceIterator
)
def
test_radioselect_num_queries
(
self
):
def
test_modelchoicefield_num_queries
(
self
):
"""
Widgets that render multiple subwidgets shouldn't make more than one
database query.
"""
categories
=
Category
.
objects
.
all
()
class
CategoriesForm
(
forms
.
Form
):
categories
=
forms
.
ModelChoiceField
(
Category
.
objects
.
all
(),
widget
=
forms
.
RadioSelect
)
radio
=
forms
.
ModelChoiceField
(
queryset
=
categories
,
widget
=
forms
.
RadioSelect
)
checkbox
=
forms
.
ModelMultipleChoiceField
(
queryset
=
categories
,
widget
=
forms
.
CheckboxSelectMultiple
)
template
=
Template
(
"""
{
%
for widget in form.checkbox
%
}{{ widget }}{
%
endfor
%
}
{
%
for widget in form.radio
%
}{{ widget }}{
%
endfor
%
}
"""
)
template
=
Template
(
'{
%
for widget in form.categories
%
}{{ widget }}{
%
endfor
%
}'
)
with
self
.
assertNumQueries
(
2
):
template
.
render
(
Context
({
'form'
:
CategoriesForm
()}))
...
...
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