API Documentation¶
Widgets¶
-
class
linked_select2.forms.
LinkedModelSelect2Mixin
(*args, **kwargs)¶ Bases:
object
Widget mixin that provides attributes and methods for
LinkedAutoResponseView
.-
build_attrs
(extra_attrs=None, **kwargs)¶ Add Django-Linked-Select2 specific data attributes.
-
filter_queryset
(term, queryset=None, linked_fields=None)¶ Return QuerySet filtered by search_fields matching the passed term.
Args: term (str): Search term. queryset (QuerySet): Basic QuerySet instance. linked_fields (dict): Map of linked keys and their values.
- Returns:
- QuerySet: Filtered QuerySet
-
form
= None¶ Form used to validate linked data. Names of fields in this form are used in the frontend.
-
get_linked_fields
()¶ Returns the names of fields whose values should be sent along with other query params during every ajax fetch.
-
get_queryset
(form=None)¶ Return QuerySet based on
queryset
ormodel
. Args: form (Form): Form containing the linked field values.- Returns:
- QuerySet: QuerySet of available choices.
-
get_search_fields
(form=None)¶ Return list of lookup names.
-
label_from_instance
(obj)¶ Return option label representation from instance.
Can be overridden to change the representation of each choice.
Example usage:
class MyWidget(ModelSelect2Widget): def label_from_instance(obj): return force_text(obj.title).upper()
- Args:
- obj (django.db.models.Model): Instance of Django Model.
- Returns:
- str: Option label.
-
max_results
= 25¶ Maximal results returned by
LinkedAutoResponseView
.
-
model
= None¶ Model of returned QuerySet. Can be None in class definition if
LinkedModelSelect2Mixin.get_queryset()
is properly overriden.
-
queryset
= None¶
-
render_options
(*args)¶ Render only selected options and set QuerySet from
ModelChoicesIterator
.
-
reset_upon_link_change
= True¶ If True, changing any value in one of linked inputs will reset the selected value and options of this widget.
-
search_fields
= []¶ Model lookups that are used to filter the QuerySet.
Example:
search_fields = [ 'title__icontains', ]
-
set_to_cache
()¶ Add widget’s attributes to Django’s cache.
Unlike Django-Select2, does not store QuerySet.
-
-
class
linked_select2.forms.
LinkedModelSelect2MultipleWidget
(*args, **kwargs)¶ Bases:
linked_select2.forms.LinkedModelSelect2Mixin
,django_select2.forms.HeavySelect2MultipleWidget
Select2 drop in model multiple select widget.
Works just like
ModelSelect2Widget
but for multi select.-
media
¶
-
-
class
linked_select2.forms.
LinkedModelSelect2TagWidget
(*args, **kwargs)¶ Bases:
linked_select2.forms.LinkedModelSelect2Mixin
,django_select2.forms.HeavySelect2TagWidget
-
media
¶
-
-
class
linked_select2.forms.
LinkedModelSelect2Widget
(*args, **kwargs)¶ Bases:
linked_select2.forms.LinkedModelSelect2Mixin
,django_select2.forms.HeavySelect2Widget
-
media
¶
-
URLs¶
Django-Linked-Select2 URL configuration.
Add linked_select2 to your urlconf
if you use any ‘Model’ fields:
url(r'^select2/', include('linked_select2.urls')),
This way, both Django-Select2 and Django-Linked-Select2 widgets will work.
Views¶
JSONResponse views for model widgets.
-
class
linked_select2.views.
LinkedAutoResponseView
(**kwargs)¶ Bases:
django.views.generic.list.BaseListView
View that handles requests from heavy model widgets and their linked versions.
The view only supports HTTP’s GET method.
-
get
(request, *args, **kwargs)¶ Return a
django.http.JsonResponse
.Example:
{ 'results': [ { 'text': "foo", 'id': 123 } ], 'more': true }
-
get_linked_fields
()¶ Extracts linked field values as query parameters from GET data.
- Returns:
- dict: Map of linked keys and their values.
-
get_linked_widget_classes
()¶
-
get_paginate_by
(queryset)¶ Paginate response by size of widget’s max_results parameter.
-
get_queryset
()¶ Get QuerySet from cached widget.
Django-Linked-Select2 querysets are reevaluated every time.
-
get_widget_or_404
()¶ Get and return widget from cache.
- Raises:
- Http404: If if the widget can not be found or no id is provided.
- Returns:
- LinkedModelSelect2Mixin or ModelSelect2Mixin: Widget from cache.
-
link_query_prefix
= None¶
-