| 1 | = !AjaxForeignKey Field = |
| 2 | |
| 3 | == The Problem == |
| 4 | In the Admin, I would like to have an autocompletion field for !ForeignKey references. I know raw-id-admin works, but AJAX is just so much cooler. |
| 5 | |
| 6 | == The Solution == |
| 7 | === Overview === |
| 8 | Using [http://developer.yahoo.com/yui/ YUI], create a new field similar to {{{models.ForeignKey}}} that renders differently in the admin. |
| 9 | |
| 10 | == Installation == |
| 11 | |
| 12 | Here's how it's setup: |
| 13 | |
| 14 | 1. Create a new folder in your project called {{{ajaxfkey }}} |
| 15 | |
| 16 | 2. Create three files in that directory: |
| 17 | |
| 18 | * {{{__init__.py}}} -- duh |
| 19 | * {{{fields.py}}} -- contains the actual model field. (download) |
| 20 | * {{{forms.py}}} -- contains the form field. (download) |
| 21 | 3. Create a {{{base_site.html}}} file in your {{{base_templates/admin}}} directory. |
| 22 | |
| 23 | 4. Modify your models to use it correctly. |
| 24 | |
| 25 | === {{{ base_templates/admin/base_site.html }}} === |
| 26 | |
| 27 | {{{ |
| 28 | {% extends "admin/base.html" %} |
| 29 | {% load i18n %} |
| 30 | |
| 31 | {% block extrahead %}{{ block.super }} |
| 32 | <script type="text/javascript" src="/media/scripts/yui/yahoo/yahoo-min.js"></script> |
| 33 | <script type="text/javascript" src="/media/scripts/yui/yahoo-dom-event/yahoo-dom-event.js"></script> |
| 34 | <script type="text/javascript" src="/media/scripts/yui/dom/dom-min.js"></script> |
| 35 | <script type="text/javascript" src="/media/scripts/yui/connection/connection-min.js"></script> |
| 36 | <script type="text/javascript" src="/media/scripts/yui/animation/animation-min.js"></script> |
| 37 | <script type="text/javascript" src="/media/scripts/yui/autocomplete/autocomplete-min.js"></script> |
| 38 | <script type="text/javascript" src="/media/scripts/json.js"></script> |
| 39 | {% endblock %} |
| 40 | |
| 41 | {% block title %}{{ title|escape }} | {% trans 'Django site admin' %}{% endblock %} |
| 42 | |
| 43 | {% block branding %} |
| 44 | <h1 id="site-name">{% trans 'Django administration' %}</h1> |
| 45 | {% endblock %} |
| 46 | |
| 47 | {% block nav-global %}{% endblock %} |
| 48 | }}} |
| 49 | |
| 50 | Notice how YUI is in {{{"/media/scripts/yui"}}} and I downloaded [http://www.json.org/json.js json.js] |