Version 1 (modified by 18 years ago) ( diff ) | ,
---|
AjaxForeignKey Field
The Problem
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.
The Solution
Overview
Using YUI, create a new field similar to models.ForeignKey
that renders differently in the admin.
Installation
Here's how it's setup:
- Create a new folder in your project called
ajaxfkey
- Create three files in that directory:
__init__.py
-- duhfields.py
-- contains the actual model field. (download)forms.py
-- contains the form field. (download)
- Create a
base_site.html
file in yourbase_templates/admin
directory.
- Modify your models to use it correctly.
base_templates/admin/base_site.html
{% extends "admin/base.html" %} {% load i18n %} {% block extrahead %}{{ block.super }} <script type="text/javascript" src="/media/scripts/yui/yahoo/yahoo-min.js"></script> <script type="text/javascript" src="/media/scripts/yui/yahoo-dom-event/yahoo-dom-event.js"></script> <script type="text/javascript" src="/media/scripts/yui/dom/dom-min.js"></script> <script type="text/javascript" src="/media/scripts/yui/connection/connection-min.js"></script> <script type="text/javascript" src="/media/scripts/yui/animation/animation-min.js"></script> <script type="text/javascript" src="/media/scripts/yui/autocomplete/autocomplete-min.js"></script> <script type="text/javascript" src="/media/scripts/json.js"></script> {% endblock %} {% block title %}{{ title|escape }} | {% trans 'Django site admin' %}{% endblock %} {% block branding %} <h1 id="site-name">{% trans 'Django administration' %}</h1> {% endblock %} {% block nav-global %}{% endblock %}
Notice how YUI is in "/media/scripts/yui"
and I downloaded json.js
Attachments (4)
-
fields.py
(414 bytes
) - added by 18 years ago.
field definition
-
forms.py
(5.0 KB
) - added by 18 years ago.
form field definition
-
views.py
(2.1 KB
) - added by 18 years ago.
Ajax View
-
forms2.py
(6.0 KB
) - added by 18 years ago.
Makes more usable without JS, supports MSIE 6 and 5.5
Download all attachments as: .zip
Note:
See TracWiki
for help on using the wiki.