Model Fields¶
The documentation foradmin_kit.modelsmodule. Currently, the module provides only one field, new fields will be added in future releases.
MultiSelectField¶
MultiSelectFieldprovides Multi Selecting features. It internally by default stores in aTextFieldand the values are seperated by,.Parameters
It accepts all thedjango model parameters. Below are the additional parameters or special behaviour for a parameter.
seperatorThe default is
,. This value will be used as a delimiter while storing the selected values.
max_lengthDefault is
null. If this is set, thenvarchar(max_length)will be used for storage by defaultTextFieldis used for storage.
choicesThe choices used for storage. This field is optional as the choices can be from an ajax class
ajax_sourceThe
keyname used while registering inheritedadmin_kit.ajax.Ajaxclass. The return of itsrunmethod will be used as choices.
ajax_targetThis will be of the form
key_name:target_field. On every change in its input, it hits the inheritedadmin_kit.ajax.Ajaxclass mapped to the specifiedkey_name. Iftarget_fieldis 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
genresfield, the selected values will be sent to ajax class mapped to key:selected-genreswhich isSelectedAjaxand its return will be filled toselectedValuesfield.As you can notice the
target_fieldofajax_targetparameter need not be fromadmin_kit.modelsmodule.
ajax_subscribeThis parameter is paired with
ajax_sourceparameter and is set toFalse. If it isTrue, then this field is linked to every other field with itsajax_sourcevalue same as its linkedajax_targetvalue.But it wont be linked to fields which have
target_fieldspecified in itsajax_targetparameter.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 fieldswill have choices dynamically filled whenevergenresfield is modified. And the choices forchosen_fieldswill be from return of theGenresAjaxclass.
kit_configThis defaults to
None. Instead of passingajax_source,ajax_targetandajax_subscribeseperately, 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¶
SelectFieldprovides Selecting features. It has similar behaviour asMultiSelectField. So below are the valid parametersParameters
max_lengthchoicesajax_sourceajax_targetajax_subscribekit_config