mirror of
https://github.com/django/django.git
synced 2025-06-07 04:29:12 +00:00
Updated doc and tests
This commit is contained in:
parent
8654903fef
commit
59bbc11034
@ -494,13 +494,13 @@ class AutocompleteMixin:
|
|||||||
|
|
||||||
url_name = "%s:autocomplete"
|
url_name = "%s:autocomplete"
|
||||||
|
|
||||||
def __init__(self, field, admin_site, attrs=None, choices=(), using=None):
|
def __init__(self, field, admin_site, attrs=None, choices=(), using=None, option_attrs=None):
|
||||||
self.field = field
|
self.field = field
|
||||||
self.admin_site = admin_site
|
self.admin_site = admin_site
|
||||||
self.db = using
|
self.db = using
|
||||||
self.choices = choices
|
self.choices = choices
|
||||||
self.attrs = {} if attrs is None else attrs.copy()
|
self.attrs = {} if attrs is None else attrs.copy()
|
||||||
self.option_attrs = {}
|
self.option_attrs = {} if option_attrs is None else option_attrs.copy()
|
||||||
self.i18n_name = get_select2_language()
|
self.i18n_name = get_select2_language()
|
||||||
|
|
||||||
def get_url(self):
|
def get_url(self):
|
||||||
|
@ -764,14 +764,14 @@ that specifies the template used to render each choice. For example, for the
|
|||||||
.. versionadded:: 5.2
|
.. versionadded:: 5.2
|
||||||
|
|
||||||
An optional dictionary containing option-specific HTML attributes to
|
An optional dictionary containing option-specific HTML attributes to
|
||||||
be set on the rendered widget.
|
be set on the options of the rendered widget.
|
||||||
|
|
||||||
.. code-block:: pycon
|
.. code-block:: pycon
|
||||||
|
|
||||||
>>> from django.forms.widgets import RadioSelect
|
>>> from django.forms.widgets import Select
|
||||||
>>> widget = RadioSelect(choices=(("J", "John"),), option_attrs={"class": "special"})
|
>>> widget = Select(choices=(("J", "John"),), option_attrs={"class": "special"})
|
||||||
>>> widget.render(name="beatle", value=["J"])
|
>>> widget.render(name="beatle", value=["J"])
|
||||||
'<div><div>\n <label><input type="radio" name="beatle" value="J" class="special" checked>\n John</label>\n\n</div>\n</div>'
|
'<select name="beatle">\n <option value="J" class="special" selected>John</option>\n\n</select>'
|
||||||
|
|
||||||
``NullBooleanSelect``
|
``NullBooleanSelect``
|
||||||
~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
@ -17,6 +17,7 @@ class AlbumForm(forms.ModelForm):
|
|||||||
Album._meta.get_field("band"),
|
Album._meta.get_field("band"),
|
||||||
admin.site,
|
admin.site,
|
||||||
attrs={"class": "my-class"},
|
attrs={"class": "my-class"},
|
||||||
|
option_attrs={"data-test": "custom", "class": "other"},
|
||||||
),
|
),
|
||||||
"featuring": AutocompleteSelect(
|
"featuring": AutocompleteSelect(
|
||||||
Album._meta.get_field("featuring"),
|
Album._meta.get_field("featuring"),
|
||||||
@ -196,3 +197,18 @@ class AutocompleteMixinTests(TestCase):
|
|||||||
AutocompleteSelect(rel, admin.site).media._js,
|
AutocompleteSelect(rel, admin.site).media._js,
|
||||||
list(expected_files),
|
list(expected_files),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_option_attrs(self):
|
||||||
|
beatles = Band.objects.create(name="The Beatles", style="rock")
|
||||||
|
form = AlbumForm(initial={"band": beatles.uuid})
|
||||||
|
widget = form["band"].field.widget.render(name="my_field", value=beatles.uuid)
|
||||||
|
self.assertInHTML(
|
||||||
|
f'<option value="{beatles.uuid}" data-test="custom" '
|
||||||
|
'class="other admin-autocomplete" data-ajax--cache="true" '
|
||||||
|
'data-ajax--delay="250" data-ajax--type="GET" '
|
||||||
|
'data-ajax--url="/autocomplete/" data-app-label="admin_widgets" '
|
||||||
|
'data-model-name="album" data-field-name="band" '
|
||||||
|
'data-theme="admin-autocomplete" data-allow-clear="false" '
|
||||||
|
'data-placeholder="" lang="en" selected>The Beatles</option>',
|
||||||
|
widget,
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user