Search code examples
ajaxdjangocustom-action

Create custom actions in Django admin site (not list actions)


I'm new to django and I'm trying to figure out how to create custom actions inside admin site. Let's say I want to create some sort of custom form with fancy ajax based ui. What I would normally do in .NET/PHP/Ruby is prepare some js code and a service that will be called via ajax and return json or even html. A more concrete example could be an auto complete box to manage a many to one relationship. What should I do to build such a system inside the django admin site? (I know there are a couple of ready to use solution for this. But I'm not interested: just for study purposes).


Solution

  • In this particular case (a fancier widget), ModelAdmin has several useful hooks like ModelAdmin.form and ModelAdmin.formfield_overrides.

    Creating a custom Django Form Widget is more elegant, but if the widget is pure JS (like many jQuery widgets), most of the time it is not worth - just override the change_form.html admin template for the Model in question.