django-admin-kit¶
Django Admin Kit adds more functionality to admin page. This includes easier Ajax Bindings, Different Widgets and a feature to Duplicate models in admin site.
Table of Contents¶
Getting Started¶
Installation¶
The module can be downloaded using python pip.
Command:
pip install django-admin-kit
Configuration¶
The app name
admin_kit
should be put at the top of installed apps in djangosettings
file.# settings.py INSTALLED_APPS = [ 'admin_kit', 'django.contrib.admin', 'django.contrib.auth', ... ]This is because, Admin Kit overrides Django change_form template. Then register the admin_kit app in root
urls
file with nameadmin_kit
# urls.py from django.conf.urls import url import admin_kit urlpatterns = [ ... url(r'^admin_kit/', admin_kit.site.urls, name="admin_kit"), ]Start the server and hit
/admin_kit/ping
url response. You will get aPONG
response if configured correctly.This ping url is enabled only in
DEBUG
Mode
Features¶
We will have a walk through of different features that Admin Kit provides.
Duplicate Button¶
This is a default feature that is added right after successfull configuration of the app.
![]()
This button is similar to
Add Another
button, but it initializes the fields with previously filled data. It is also compatible with django-nested-adminTo disable this feature set
KIT_DISABLE_DUPLICATE = True
in settings file.Note
The duplicate button is only on Inline Admin Models like
Staked Inline
,Tabular Inline
ornested_admin fields
.
Multi Select Field¶
Admin Kit provides Multi Select field where you can specify choices. It uses
admin_kit.models.MultiSelectField
.In models.py file
# models.py from admin_kit.models import MultiSelectField class Book(models.Model): ... GENRES = ( ('thriller', 'thriller'), ('sci-fi', 'sci-fi'), ('fictional', 'fictional'), ('fantasy', 'fantasy'), ('philosophy', 'philosophy') ) ... genres = MultiSelectField(verbose_name='Valid Genres', choices=GENRES)In Admin Panel
![]()
Ajax Binding¶
The core feature of Admin-Kit is the support for easier ajax behaviour. It binds the form-field with user defined view through ajax.
Setting up this behaviour is 2 step process.
- Step 1: API Creation
Create an
ajax.py
file in the app. And create a class that inheritsadmin_kit.ajax.Ajax
and hasrun(self, request)
method. This method is executed, which acts as an API.And register this class using
admin_kit.site.register
method. The first argument is the key through which the model links to class and second is the class itself.For our example lets fill the choices from an API. Create an
ajax.py
with below code.import admin_kit class GenresAjax(admin_kit.ajax.Ajax): def run(self, request): GENRES = ( ('thriller', 'thriller'), ('sci-fi', 'sci-fi'), ('fictional', 'fictional'), ('fantasy', 'fantasy'), ('philosophy', 'philosophy') ) return GENRES admin_kit.site.register('genres', GenresAjax)Internally, the return type of
run
method is json formatted and acts as an API.You can get the response by hitting
admin_kit/ajax/genres
. Heregenres
in the url is same as thekey
name used for registering in ajax.py file.![]()
The data was rendered by Chrome Extension JSON View
Step 2: Model Binding
In our
models.py
file modifygenres
field with below codegenres = MultiSelectField(verbose_name='Valid Genres', ajax_source='genres')And thats it!! you will get the same behaviour, but now the choices are filled from your function. For every change in value, it calls
run
method from your ajax class. Thus you can process the return based on the request.You can also access the user selected values and target the values to a specific field. To learn them please go through our documentation
Gotchas¶
- While using ajax behaviour make sure the model field is from
admin_kit.fields
. If you try to use ajax attributes likeajax_source
orkit_config
in fields fromdjango.models
, you will get an error- As the project is new, currently it only has MultiSelectField. In further releases, newer fields will be integrated.
Documentation¶
The documentation consists of mainly five parts.
Model Fields¶
The documentation foradmin_kit.models
module. Currently, the module provides only one field, new fields will be added in future releases.
MultiSelectField¶
MultiSelectField
provides Multi Selecting features. It internally by default stores in aTextField
and the values are seperated by,
.Parameters
It accepts all thedjango model parameters
. 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¶
SelectField
provides Selecting features. It has similar behaviour asMultiSelectField
. So below are the valid parametersParameters
max_length
choices
ajax_source
ajax_target
ajax_subscribe
kit_config
Form Fields¶
The documentation foradmin_kit.fields
module.
MultiSelect Form Field¶
MultiSelectField
provides Multi Selecting features. It is same as models.MultiSelectField, but is used inDjango Admin Forms
Parameters
seperator
The default is
,
. This value will be used as a delimiter while storing the selected values.
choices
The choices used for rendering. 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.
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.
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
# admin.py ... from admin_kit.fields import MultiSelectField class BookForm(forms.ModelForm): model = Book selected_fields = MultiSelectField(ajax_source='genres', ajax_subscribe=True) fields = ('name', 'genres') class BookAdmin(nested_admin.NestedStackedInline): model = Book form = BookFormis equivalent to
# admin.py ... from admin_kit.fields import MultiSelectField class BookForm(forms.ModelForm): model = Book kit_config = { 'ajax-source': 'genres', 'ajax-subscribe': True } selected_fields = MultiSelectField(kit_config=kit_config) fields = ('name', 'genres') class BookAdmin(nested_admin.NestedStackedInline): model = Book form = BookFormNote
Make sure the key names are hiphen seperated.
Select Form Field¶
SelectField
provides Selecting features. It is similar toMultiSelectField
, but provides a single value to select.Parameters
choices
ajax_source
ajax_target
ajax_subscribe
kit_config
Form Widgets¶
The documentation foradmin_kit.widgets
module.
SelectMultipleWidget¶
MultiSelectField
is the widget used for Multi Select. It inheritsDjango SelectMultiple Widget
class.The widget doesnt take any new parameters. It just adds the initial value of that widget to
data-kit-config
attribute.
SelectWidget¶
SelectWidget
is the widget used for Select. It inheritsDjango Select Widget
class. This widget is similar toMultiSelectField
.
Ajax Module¶
The documentation foradmin_kit.ajax
module. The module has only one classAjax
. So we will go through its attributes , methods and their functionality.
Attributes¶
-
response_type
¶ The response type of the ajax class. Its defaults to
json
, where its jsonifies the output python object. It also acceptstext
where it converts the output to a string and is sent as the response.
-
-
unique
¶ It defaults to
False
, if itsTrue
then the key will be unique to a class. Hence different Ajax classes with the same key can be registered.
-
Methods¶
-
run
(self, request)¶ The main method that will be executed for generating response for an Ajax request. This is method should be overided by the child class.
-
Note
The remainder methods are internal. So should be overrided only if necessary.
-
route
(self, request)¶ This is the core function, that calls
run
method and then passes the output toformat_response
method and returns it. This method is executed when theadmin_kit site
figures out theajax_class
based on the request.
-
-
classmethod
generate_key
(cls, key)¶ Generates the key based on the configuration of Ajax Class. If the
unique
attribute is set , it prepends thekey
with the slug form of its class name.This method is called in the
register
function forkey
andajax_class
mapping.Example
# ajax.py from admin_kit import ajax class TestAjax(ajax.Ajax): ... class UniqueTestAjax(ajax.Ajax): unique = True ... TestAjax.generate_key('key') # `key` UniqueTestAjax.generate_key('key') # `unique-test-ajax-key`
To access this Ajax class in
models
, Its slugged key name has to be used. In the above Example to map toUniqueTestAjax
class,unique-test-ajax-key
key should be used inmodels
file.
-
classmethod
Sites Module¶
The documentation of
admin_kit.sites
module. The module has only classAdminKitSite
which is the root site of the app.This site object is aliased to
admin_kit.site
. So it can accessed through the same
Site Methods¶
-
register
(key, ajax_class)¶ - key :: str
This is the key that will be used in models for binding
- ajax_class :: class
The ajax class that inherits
admin_kit.ajax.Ajax
This method is used to bind an
ajax_class
to akey
.Note
If
unique
attribute ofajax_class
isTrue
, remember to prepend its slugname to thekey
.
-
Contents¶
Models¶
Admin Kit Models module
-
class
admin_kit.models.
BaseField
(kit_config=None, ajax_source=None, ajax_target=None, ajax_subscribe=False, *args, **kwargs)[source]¶ The Base model field of Admin-Kit models. This inherits Django’s models.Field class.
-
deconstruct
()[source]¶ Deconstructs the field to a tuple of 4 elements. This is used to recreate the same object.
-
formfield
(form_class=None, choices_form_class=None, **kwargs)[source]¶ Returns the form object to be used for rendering.
-
-
class
admin_kit.models.
MultiSelectField
(seperator=', ', *args, **kwargs)[source]¶ The Multiselect model field of Admin-Kit, which allows users to create multi select ajax fields.
Fields¶
Admin Kit Fields module
-
class
admin_kit.fields.
BaseField
(kit_config=None, ajax_source=None, ajax_target=None, ajax_subscribe=None, *args, **kwargs)[source]¶ The Base Field for form fields
Widgets¶
Admin Kit Widgets module
Site¶
Admin Kit Sites module
-
class
admin_kit.sites.
AdminKitSite
(name='admin_kit')[source]¶ The main AdminKitSite that routes and process url requests.
-
register
(key, ajax_class)[source]¶ Registers the
ajax_class
for ajax behaviour- key :: str
- This is the key that will be used in models for binding
- ajax_class :: class
- The ajax class that inherits
admin_kit.ajax.Ajax
-
urls
¶ The actual property used by django for routing requests
-
-
admin_kit.site.
register
(key, ajax_class)¶ Registers the
ajax_class
for ajax behaviour. This is same asadmin_kit.sites.AdminKitSite.register
method- key :: str
- This is the key that will be used in models for binding
- ajax_class :: class
- The ajax class that inherits
admin_kit.ajax.Ajax
Ajax¶
Admin Kit Ajax module
-
class
admin_kit.ajax.
Ajax
[source]¶ This is the base class for Ajax functionality.
- response_type : str
- The response type of the API. By default its set to
json
, It also acceptstext
. - unique : bool
- If True, the
key
is prepended with class name slug, Thus making it unique per class.
-
classmethod
generate_key
(key)[source]¶ A class method that generates key, that maps to the function
If
unique
attribute is true, then it appends hiphen seperated class name to actualkey
Example:
>>> import DummyAjaxClass >>> DummyAjaxClass.generateKey('the_key') the_key >>> DummyAjaxClass.unique = True >>> DummyAjaxClass.generateKey('the_key') dummy-ajax-class-the_key
Index¶
Python Module Index¶
Liscense¶
The django code is licensed under The MIT License. View the license file to under the root directory for complete license and copyright information.