The documentation for
admin_kit.models
module. Currently, the module provides only one field, new fields will be added in future releases.
MultiSelectField<admin_kit.models.MultiSelectField>
provides Multi Selecting features. It internally by default stores in aTextField
and the values are seperated by,
.Parameters
It accepts all the
django model parameters<django.db.models.fields>
. Below are the additional parameters or special behaviour for a parameter.
seperator
The default is
,
. This value will be used as a delimiter while storing the selected values.
max_length
Default is
null
. If this is set, thenvarchar(max_length)
will be used for storage by defaultTextField
is used for storage.
choices
The choices used for storage. This field is optional as the choices can be from an ajax class
ajax_source
The
key
name used while registering inheritedadmin_kit.ajax.Ajax
class. The return of itsrun
method will be used as choices.
ajax_target
This will be of the form
key_name:target_field
. On every change in its input, it hits the inheritedadmin_kit.ajax.Ajax
class mapped to the specifiedkey_name
. Iftarget_field
is specified then it sets the value of specified model field to api's return value.The selected values will be passed as
q[]
query list parameter.Example
# ajax.py import admin_kit class SelectedGenresAjax(admin_kit.ajax.Ajax): response_type = 'text' def run(self, request): query = request.GET.getlist('q[]') response = ','.join(query) return response admin_kit.site.register('selected-genres', SelectedGenresAjax)This ajax class, joins query list
q[]
values with,
and returns it in text format.# models.py from admin_kit.models import MultiSelectField class Book(models.Model): name = models.CharField(max_length=100) GENRES = ( ('thriller', 'thriller'), ('sci-fi', 'sci-fi'), ('fictional', 'fictional'), ('fantasy', 'fantasy'), ('philosophy', 'philosophy') ) genres = MultiSelectField(verbose_name='Valid Genres', choices=GENRES, ajax_target='selected-genres:selectedValues') selectedValues = models.TextField(verbose_name='Selected Values')Hence for every change in
genres
field, the selected values will be sent to ajax class mapped to key:selected-genres
which isSelectedAjax
and its return will be filled toselectedValues
field.As you can notice the
target_field
ofajax_target
parameter need not be fromadmin_kit.models
module.
ajax_subscribe
This parameter is paired with
ajax_source
parameter and is set toFalse
. If it isTrue
, then this field is linked to every other field with itsajax_source
value same as its linkedajax_target
value.But it wont be linked to fields which have
target_field
specified in itsajax_target
parameter.Example
# ajax.py class GenresAjax(ajax.Ajax): def run(self, request): query = request.GET.getlist('q[]') response = list(zip(query, query)) return responseThis ajax class zips the selected query and returns it back.
# models.py class Book(models.Model): ... genres = MultiSelectField(verbose_name='Valid Genres', choices=GENRES, ajax_target='genres') chosen_fields = MultiSelectField(seperator=',', ajax_source='genres', ajax_subscribe=True)Here
chosen fields
will have choices dynamically filled whenevergenres
field is modified. And the choices forchosen_fields
will be from return of theGenresAjax
class.
kit_config
This defaults to
None
. Instead of passingajax_source
,ajax_target
andajax_subscribe
seperately, one can specify them in a dictionary and can be passed to this parameter.Example
# models.py class Book(models.Model): ... genres = MultiSelectField(verbose_name='Valid Genres', ajax_source='genres', ajax_subscribe=True, ajax_target='selected-genres:selectedValues')Is equivalent to
# models.py class Book(models.Model): ... kit_config = { 'ajax-source': 'genres', 'ajax-subscribe': True, 'ajax-target': 'selected-genres:selectedValues' } genres = MultiSelectField(verbose_name='Valid Genres', choices=GENRES, kit_config=kit_config)Note
Make sure the key names are hiphen seperated and not underscore seperated.
SelectField<admin_kit.models.SelectField>
provides Selecting features. It has similar behaviour asMultiSelectField<admin_kit.models.MultiSelectField>
. So below are the valid parametersParameters
max_length
choices
ajax_source
ajax_target
ajax_subscribe
kit_config