text: 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, but AJAX is just so much cooler.
  The Solution
  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
  - Put four files in that directory:
  __init__.py-- duh
  fields.py-- contains the actual model field. (download)
  forms.py-- contains the form field. (download)
  To use, you need to define a
  ajax_autocomplete(data) (either in
  Model or
  Model.objects) and an
  ajax_str.
  Below is an example usage in some models:
  from django.db import models from ajaxfkey.fields import AjaxForeignKey # notice the manager, we can create a staticmethod in Poll if we wanted. class PollManager(models.Manager): def ajax_autocomplete(self, data): return self.filter(question__istartswith = data).order_by('question') class Poll(models.Model): question = models.CharField(maxlength=200) pub_date = models.DateTimeField('date published') # set the manager objects = PollManager() # this is how it displays in the autocompletion...should be related to # how the person filters above def ajax_str(self): return self.question class Choice(models.Model): # notice it's the same syntax as models.ForeignKey poll = AjaxForeignKey(Poll) choice = models.CharField(maxlength=200) votes = models.IntegerField()
  Screenshot
  Congratulations, you should be done! The obligatory screenshot is:
  Problems
  If you have any problems, feel free to email me at
  Attachments (4)
  - fields.py (414 bytes) - added by 10 years ago.
  field definition
  - forms.py (5.0 KB) - added by 10 years ago.
  form field definition
  - views.py (2.1 KB) - added by 10 years ago.
  Ajax View
  - forms2.py (6.0 KB) - added by 10 years ago.
  Makes more usable without JS, supports MSIE 6 and 5.5
  Download all attachments as: .zip