= Autocompletion for !ForeignKeys in Admin (YUI) = == The Problem == In the Admin, I would like to have an autocompletion field for !ForeignKey references. I know {{{raw_id_admin}}} works for foreign keys to big tables, but AJAX is just so much cooler. == The Solution == Using [http://developer.yahoo.com/yui/ YUI], create a new field similar to {{{models.ForeignKey}}} that renders differently in the admin. == Installation == Here's how it's setup: 1. Create a new folder in your project called {{{ajaxfkey }}} 2. Put four files in that directory: * {{{__init__.py}}} -- duh * {{{fields.py}}} -- [#a2.bFixingfields.py contains the actual model field. (download)] * {{{forms.py}}} -- contains the form field. (download '''forms2.py''' and rename) * {{{views.py}}} -- contains the AJAX view. (download) 3. [#a3.Creatingbase_templatesadminbase_site.html Create a base_site.html file in your base_templates/admin directory.] 4. [#a4.Modifyurls.py Modify your urls conf to contain the ajax view.] 5. [#a5.Usingitinyourmodels Modify your models to use it correctly.] [#Installation (top)] === 2.b Fixing {{{fields.py}}} === If you downloaded {{{fields.py}}}, you need to make sure the import line at the top correctly imports the Form field. You might need to change this {{{ #!python from ajaxfkey.forms import AjaxForeignKeyFormField }}} to this {{{ #!python from myproject.ajaxfkey.forms import AjaxForeignKeyFormField }}} [#Installation (top)] === 3. Creating {{{base_templates/admin/base_site.html}}} === Put the contents below in a place locatable by {{{'admin/base_site.html'}}} in your setup. (It certainly does not need to be inside Django's source.) {{{ {% extends "admin/base.html" %} {% load i18n %} {% block extrahead %}{{ block.super }} {% endblock %} {% block title %}{{ title|escape }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %}